非常新手程序员,对不起,如果这是愚蠢的或者我的英语是错误的。所以,我有我正在写的这个命令行通讯录。它由一个字典组成,该字典包含一个以键作为名称变量的对象,并且每个对象都有与之关联的变量,例如人名,电子邮件等......它可以工作,但现在我正在尝试制作它使用pickle将字典持久存储在内存中。
def create_person():
"""Adds an instance object of the Person class to the dictionary persons. persons is a global variable, that has been created previously. DATA is a variable that points to a file named test.data that exists in the same directory as the script."""
name = raw_input("Enter the person's name here: ")
email = raw_input("Enter the person's email here: ")
phone = raw_input("Enter the person's phone here: ")
address = raw_input("Enter the person's address here: ")
f = open(DATA, "rb")
persons = pickle.load(f) #assign whatever is saved in the dictionary in persistent memory to global variable persons, which is empty at this point in the beginning
f.close()
persons[name] = Person(name, email, phone, address)
f = open(DATA, "wb")
pickle.dump(persons, f)
f.close()
但是,我收到此错误:
Traceback (most recent call last):
File "testpickle.py", line 85, in <module>
main()
File "testpickle.py", line 40, in main
create_person()
File "testpickle.py", line 20, in create_person
persons = pickle.load(f)
File "/home/pedro/anaconda/lib/python2.7/pickle.py", line 1378, in load
return Unpickler(file).load()
File "/home/pedro/anaconda/lib/python2.7/pickle.py", line 858, in load
dispatch[key](self)
File "/home/pedro/anaconda/lib/python2.7/pickle.py", line 880, in load_eof
raise EOFError
EOFError
我不明白这一点。我实际上已经写了这个程序,它正在使用内存节省,但我不小心删除了它。怎么了?