从空泡菜文件中解泡 Python 字典时出现 EOFerror

EOFerror in unpickling Python dictionary from an empty pickle file

非常新手程序员,如果这很愚蠢或者我的英语有误,请原谅。所以,我有我正在写的这个命令行地址簿。它由一个字典组成,该字典包含一个以键为名称变量的对象,每个对象都有与之关联的变量,例如人名,电子邮件等......它有效,但现在我正在尝试它使用 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

我不明白这个。这个程序我其实早就写好了,而且是在节省内存的情况下工作的,但是我不小心把它删了。发生了什么事?

我没有发现你的代码有什么明显的错误,但如果你只是想像这样存储 key/value 对,你可能应该使用 shelve 模块而不是 home-酿造溶液。

import shelve

persons = shelve.open(DATA)

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: ")
    persons[name] = Person(name, email, phone, address)    

while more_people:
    create_person()

persons.close()    

pickle 点击 EOF 肯定会告诉文件已损坏(可能被截断,可能写入操作失败)。

如果您将它上传到某处,我们也许可以推断出它到底出了什么问题。在最坏的情况下,您将不得不窥视内部并推断出那里的数据,然后手动重新输入它。

这是您使用不可读格式和不注意其完整性的代价(例如 writing to another file and only moving it over the original one after saving succeeded)。

更新:

如果你想从一个新的 "empty"文件开始,然后处理文件丢失的情况并产生一个空的dict。毕竟,文件 最初应该丢失的,不是吗?

空文件不是有效的 pickle 数据(它必须至少包含有关对象类型的信息)。

这里是 "one obvious way" ((c) Python Zen) 来处理丢失的文件:

import errno    
try: f = open(DATA, "rb")
except IOError,e:
    if e[0]==errno.ENOENT: persons={}
    else: raise
else:
    persons = pickle.load(f)
    f.close()
    del f