从 python 中的文本文件中提取信息
extracting info from a text file in python
我正在制作一个系统,当用户玩我的游戏时,它会使用用户名和密码将他们的分数与之前的分数进行比较。信息以以下格式存储在文本文件中:score*username*password
。我试图将它们放入不同的变量中,以便我可以根据其他变量检查它们。这是我正在使用的代码。问题是变量的分配不起作用,我无法弄清楚为什么
username = "hello"
password = "1234"
player_score = 40
file = open("yahtzee high scores.txt", "r")
lines = file.readlines()
file.close()
highscore = 0
highUser = ""
print(lines)
for line in lines:
score = ""
name = ""
passw = ""
findingScore = True
findingName = False
findingPass = False
for i in line:
if i != "*" and findingScore:
score += i
elif i != "*" and findingName:
name += i
elif i != "*" and findingPass:
passw += i
else:
print(name)
if findingScore:
findingName = True
findingScore = False
elif findingName:
findingName = False
findingPass = True
elif findingPass:
findingPass = False
if int(score) > highscore:
highscore = int(score)
highUser = name
highPass = passw
print(highscore)
print(highUser)
使用起来效率更高(也更容易)json
使用以下作为 file.json
{
"user1": {"password":"password","high score":50},
"user2": {"password":"password","high score":30}
}
import json
data = json.load(open('file.json', "r"))
print(data["user1"]["high score"])
#assign new score
data["user1"]["high score"] = 60
# add new user
password = "user3Password123"
score = 30
data["user3"] = {
"password": password,
"high score": score
}
json.dump(data, open('file.json', "w"))
文件现在
{
"user1": {"password":"password","high score":60},
"user2": {"password":"password","high score":30},
"user3": {"password": "user3Password123", "high score": 30}
}
原来有一个函数 string.split("*")
可以创建一个包含密码和用户名的列表。
ps。不确定拆分函数的确切语法,但确实存在
我正在制作一个系统,当用户玩我的游戏时,它会使用用户名和密码将他们的分数与之前的分数进行比较。信息以以下格式存储在文本文件中:score*username*password
。我试图将它们放入不同的变量中,以便我可以根据其他变量检查它们。这是我正在使用的代码。问题是变量的分配不起作用,我无法弄清楚为什么
username = "hello"
password = "1234"
player_score = 40
file = open("yahtzee high scores.txt", "r")
lines = file.readlines()
file.close()
highscore = 0
highUser = ""
print(lines)
for line in lines:
score = ""
name = ""
passw = ""
findingScore = True
findingName = False
findingPass = False
for i in line:
if i != "*" and findingScore:
score += i
elif i != "*" and findingName:
name += i
elif i != "*" and findingPass:
passw += i
else:
print(name)
if findingScore:
findingName = True
findingScore = False
elif findingName:
findingName = False
findingPass = True
elif findingPass:
findingPass = False
if int(score) > highscore:
highscore = int(score)
highUser = name
highPass = passw
print(highscore)
print(highUser)
使用起来效率更高(也更容易)json
使用以下作为 file.json
{
"user1": {"password":"password","high score":50},
"user2": {"password":"password","high score":30}
}
import json
data = json.load(open('file.json', "r"))
print(data["user1"]["high score"])
#assign new score
data["user1"]["high score"] = 60
# add new user
password = "user3Password123"
score = 30
data["user3"] = {
"password": password,
"high score": score
}
json.dump(data, open('file.json', "w"))
文件现在
{
"user1": {"password":"password","high score":60},
"user2": {"password":"password","high score":30},
"user3": {"password": "user3Password123", "high score": 30}
}
原来有一个函数 string.split("*")
可以创建一个包含密码和用户名的列表。
ps。不确定拆分函数的确切语法,但确实存在