[英]How to overwrite the dump/load methods in the pickle class - customizing pickling and unpickling - Python
[英]Python 3: Pickling and UnPickling class instances returning “no persistent load” error
我正在尝试制作一个程序,收集大量关于乐队中某些玩家何时可以在这个圣诞节玩弄的数据,我正在努力让pickle功能做我想做的......数据存储在Player
下面的类的类实例:
import pickle
class Player():
def __init__(self, name, instrument, availability):
self.Name=name
self.Instrument=instrument
self.Availability=availability
播放器列表, PlayerList
被定义为一个空列表,我已经定义了一个函数AddPlayer
,它将初始化一个类实例,并将播放器的详细信息存储为属性...
PlayerList=[]
def AddPlayer(PlayerList, name, instrument, availability):
NewPlayer = Player(name, instrument, availability)
PlayerList.append(NewPlayer)
print("Player "+name+" has been added.\n\n")
然后,我有一个功能,用于存储用户退出程序时的播放器列表...
def StartProgram(PlayerList):
while True:
choice=input("Would you like to:\n1 Add a Player?\n2 Quit?\n")
if choice=="1":
## Adds the details of the Player using the above function
AddPlayer(PlayerList, "Test Player", "Instrument", ["1st Dec AM"])
StartProgram(PlayerList)
elif choice=="2":
file=open("BuskingList.txt", "wb")
file=open("BuskingList.txt", "ab")
def AddToList(PlayerList):
print("PlayerList: "+str(PlayerList))
HalfPlayerList=PlayerList[:5]
## For some reason, pickle doesn't like me trying to dump a list with more than
## 5 values in it, any reason for that?
for Player in HalfPlayerList:
print("Player: "+str(Player))
PlayerList.remove(Player)
## Each player removed from original list so it's only added once.
print("HalfPlayerList: "+str(HalfPlayerList))
pickle.dump(HalfPlayerList, file)
if len(PlayerList) !=0:
AddToList(PlayerList)
## Recursive function call while there are still players not dumped
AddToList(PlayerList)
file.close()
quit()
else:
print("Enter the number 1, 2, or 3.\n")
StartProgram(PlayerList)
最后一个函数在程序开始时运行,以加载所有玩家的信息......
def Start():
file=open("BuskingList.txt", "rb")
print("File contains: "+str(file.readlines()))
PlayerList=[]
CheckForPlayers=file.read()
if CheckForPlayers!="":
file=open("BuskingList.txt", "rb")
ListOfLists=[]
for line in file:
ToAppend=pickle.load(file)
ListOfLists.append(ToAppend)
for ListOfPlayers in ListOfLists:
for Player in ListOfPlayers:
PlayerList.append(Player)
StartProgram(PlayerList)
print("When entering dates, enter in the form 'XXth Month AM/PM'\n")
Start()
当程序第一次运行时(提供BuskingList.txt
存在),程序运行正常,添加一个名称工作并将其腌制并在退出时将其转储显然有效。 但是,当程序重新启动时,它无法读取存储的数据,错误如下...
File contains: [b'\x80\x03]q\x00c__main__\n', b'Player\n', b'q\x01)\x81q\x02}q\x03(X\x04\x00\x00\x00Nameq\x04X\x0b\x00\x00\x00Test Playerq\x05X\n', b'\x00\x00\x00Instrumentq\x06h\x06X\x0c\x00\x00\x00Availabilityq\x07]q\x08X\n', b'\x00\x00\x001st Dec AMq\tauba.']
Traceback (most recent call last):
File "I:/Busking/Problem.py", line 63, in <module>
Start()
File "I:/Busking/Problem.py", line 54, in Start
ToAppend=pickle.load(file)
_pickle.UnpicklingError: A load persistent id instruction was encountered,
but no persistent_load function was specified.
我做了一些研究,发现这个持久的id malarkey应该不是问题,为什么它出现在这里? 另外,为什么酸洗时列表的五个值限制? 任何帮助,将不胜感激。
您首先使用.readlines()
读取列表:
print("File contains: "+str(file.readlines()))
然后尝试再次阅读:
CheckForPlayers=file.read()
这不起作用; 文件指针现在位于文件的末尾。 倒带或重新打开文件:
file.seek(0) # rewind to start
这不是你需要检查文件内容; 让pickle
为你做那件事。
接下来,您逐行阅读文件:
for line in file:
ToAppend=pickle.load(file)
这不起作用; pickle格式是二进制的 ,不是面向行的,你通过使用迭代读取 ,然后通过传入文件对象再次读取 。
将文件完全读取到pickle
模块:
with open("BuskingList.txt", "rb") as infile:
ToAppend=pickle.load(file)
你还在你的代码中提到:
## For some reason, pickle doesn't like me trying to dump a list with more than
## 5 values in it, any reason for that?
Pickle对任何列表大小没有任何问题 ,没有理由将你的玩家列表分成五个块。 您没有说明遇到的问题,但列表中的项目数量不是原因:
>>> import pickle
>>> with open('/tmp/test.pickle', 'wb') as testfile:
... pickle.dump(range(1000), testfile)
...
>>> with open('/tmp/test.pickle', 'rb') as testfile:
... print len(pickle.load(testfile))
...
1000
这存储并重新加载了1000个整数的列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.