Python字典酸洗

时间:2016-02-03 12:15:19

标签: python

这是我的代码,但不能使用pickle来加载它后,也认为这是非常低效的,谢谢。 写文件:

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)

2 个答案:

答案 0 :(得分:0)

尝试将每个对象转储到单独的文件中:

Type

答案 1 :(得分:0)

我通过在“r”中添加b来解决问题,我认为这不是最有效的方式,但是,它只是werkz ...

  

打开(“Tennis Scores.txt”,“r b ”)作为: