![](/img/trans.png)
[英]How to create a Python dictionary from data imported from a text file?
[英]How to create a dictionary and save the data to a text file? (Quiz game python)
这是python,我使用的是空闲版本3.4.2。 因此,目前我的代码可以运行,但我希望对其进行调整,以便能够:为该名称保存最新的三个分数(因为它们重新运行代码,并且分数保存在文本文件中)。 但是,存在一个问题,无论输入什么“名称”,它都会与分数一起保存在自己的行中,而不是“追加”(我知道这是因为它没有以列表/字典的形式保存,但是怎么保存?是吗-或我读到您可以“拆分”线路?)
这是我的第一个问题,如果有人能帮助我,我将非常感激,我是python的新手,所以这对我来说是个挑战! 任何意见或建议都非常欢迎!!
import random #import module
print("What is your name?") #prints writing in brackets
name = input().title() #Capitalizes the first letter of the word inputted
print("What class are you in? (Enter 1, 2 or 3)") #asks the user to input a number
while True:
try:
class_number = int(input()) #asks for an integer input from user
except ValueError:
print("Sorry, I didn't understand that, please try again") #print statement
continue
if class_number > 3: #if input is more than 3
print("SORRY but that class isn't recognised, try again") #print statement
continue
else:
print ("Hello,", name, "from class", class_number, "welcome to my quiz") #prints writing in brackets and anything saved in the variable "name" and "class_number"
break #break out of loop
score = 0 #sets the variable "score" to zero
question = 0 # sets the variable "question" to zero
while question < 3:#If questions (intitally set to 0) is smaller than 10, carry out this function
question +=1 # add one to the value of "question"
maths = random.randint(1,3) #randomly generate a number from 1-3 and store as "maths"
num1 = random.randint(1,10)#randomly generate an integer from 1-10 and store as "num1"
num2 = random.randint(1,10)#randomly generate a second integer from 1-10 and store as "num2"
if maths == 1: #if the number generated is 1
print(num1, "+", num2) #prints num1 + num2
ans = num1 + num2 #sets "ans" to equal the value of num1 added to num2
elif maths == 2: #if the number generated is 1
print(num1, "*", num2) #print num1 multiplied by num2
ans = num1 * num2 #sets "ans" to equal the value of num1 multiplied by num2
else: #else run this part of code
print(num1, "-", num2) #print num1 subtracted by num2
ans = num1 - num2 #sets "ans" to equal the value of num1 subtracted by num2
while True:
try:
user_ans = int(input()) #user inputs answer to question
except ValueError: #runs when the user input is no an integer
print ("SORRY but that answer isn't recognised, try again")
else:
break
if user_ans == ans:
score+=1
print("Well done, you are CORRECT")
else:
print("SORRY, you are INCORRECT") #print writing in brackets
print("The correct answer was", ans)
if score == 10: #run this part of code if "score" equals 10
print("fantastic", name, "you got full marks!") #print statement and name
elif score >= 6: #run this part of code if "score" is larger than or equal to 6
print("well done, there's some improvement to be done here though", name, "you got", score, "/10")# then print statement and score
elif score <=5: #run this part of code if "score" is smaller than or equal to 5
print("hmm, maybe some more practise would be beneficial", name, "you got", score, "/10") #then print statement and score
class_number = str(class_number) + ".txt" #this adds '.txt' to the end of the file (therefore creating a text file) so it can be used to create a file under the name of the class
file = open(class_number, 'a') #opens the file in 'append' mode so you don't delete all the information
file.write(str(name + " : ")) #writes the name and ":" to file
file.write(str(score)) #writes the score to file
file.write('\n')#writes the score to the file
file.close()#safely closes the file to save the information
view = int(input("would you like to view the scores? If yes, press 1 or not press 2"))
if view == 1:
exit
elif view == 2:
exit #to be completed
如果要将用户的分数保存到列表中...
my_list = []
my_list.append(score)
您可能还需要在游戏开始之前读入文件。 然后,您将需要使用文本文件中的分数填充列表。
with open(class_number) as scorefile:
for line in scorefile:
my_list.append(line)
最后,当游戏结束时,您需要将用户的分数添加到文件中。 在这种情况下,我将保持简单,并且不包括示例的人名,尽管如此,您仍然可以将行添加到列表中。
f = open(class_number 'w')
f.write("\n" + score)
f.close()
然后,如果他们查看分数,只需打印出my_list的值,并用新行将列表中的每个元素分开。
如果要使其易于阅读,请将其写入json文件:
import json
your_dictonary = {"my_key":"one_value"}
str_to_save = json.dumps(your_dictonary)
# Then write it to a file as you did before
加载几乎一样容易:
json.loads(file_content) # after you read the file as usual
使用pickle
模块存储数据。 将user => score
关系组织到字典中,然后调用pickle.dump(filename, theDict)
将其保存到文件中。
当需要它时,调用theDict = pickle.load(filename)
,它将从pickle文件中加载数据。 这种方法比必须设计自己的解析算法更好。
如果可以更改文件的外观,请尝试将文件另存为json 。
在代码的顶部,从文件中加载分数:
import json
with open('{}.txt'.format(class_number)) as f:
scores = json.load(f)
然后,您可以在字典中修改分数并写回文件:
scores[name] = score
with open('{}.txt'.format(class_number)) as f:
json.dump(scores, f)
该代码存储的最新分数不多,但是您可以探索为每个名称保存多个分数的选项。 我建议看一下defaultdict 。
一些答案建议将分数存储为JSON(好主意)或腌制(坏主意! 这很危险 )。 该答案假定您要保留平面文件,最旧优先的格式。
您将以追加模式打开文件,因此将无法更改任何内容。 要进行更改,您首先需要读取文件:
try:
fileh = open(class_number, 'r')
oldlines = fileh.readlines()
fileh.close()
except IOError: # If we didn't find the file, there were no old scores
oldlines = []
注意,这将读取所有行; 如果文件变得非常大,那么它就无法全部放入内存中,您应该逐行阅读并随心所欲地写。
对于每一行:
如果我们从最新的行开始,跟踪最近的分数会稍微容易一些,所以让我们这样做:
updatedlines = []
recentscores = 0
for line in oldlines[::-1]: # go backwards, most recent line first
(lname, lscore) = line.split(" : ") # read the name and the score from the line; this will break if the name contains " : "
if lname == name:
recentscores += 1 # keep track of how many scores we have for this person
if recentscores <3:
updatedlines.append(line)
else:
updatedlines.append(line)
updatedlines = updatedlines[::-1] # put the lines back in the right order
最后,添加新分数。
updatedlines.append("%s : %d\n" % (name, score))
现在我们可以将这些行写回到文件中:
fileh = open(class_number, 'w')
fileh.writelines(updatedlines)
fileh.close()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.