这是我的代码,但不能用pickle加载它后,也认为这是相当低效,谢谢。 写入文件:Python字典酸洗
import pickle
cont="yes"
u_ele_stu={}
ele_pl_stu={}
x=len(u_ele_stu)
y=len(ele_pl_stu)
with open("Tennis Scores.txt" ,"wb") as a:
while cont=="yes":
age=input("Would you like to add to under 11 list or 11-16? (under 11/11-16)")
if age=="under 11":
name=input("Input the name of a student: ")
pos=str(input("Input the last position they achieved in a tournament"))
u_ele_stu[x+1]=name, " ", pos
elif age=="11-16":
name=input("Input the name of a student: ")
pos=str(input("Input the last position they achieved in a tournament"))
ele_pl_stu[y+1]=name, " ", pos
cont=input("Would you like to add another student? yes/no")
cont.lower()
if cont!="yes":
pickle.dump(u_ele_stu, a)
pickle.dump(ele_pl_stu, a)
读取文件:
import pickle
with open("Tennis Scores.txt", "r") as a:
b=pickle.load(a)
c=pickle.load(a)
print(b)
print(c)
你确定你的缩进是正确的? – erip
也不要在文件名中使用空格。或者至少逃离空间。 – erip
你认为它比较低效吗? – msw