我正在尝试制作一个程序,以收集大量有关乐队中某些演奏者今年圣诞节何时可用的数据,而我正在努力让泡菜功能执行我想要的操作...数据存储在下面的类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恶意代码不应该成为问题,为什么在这里出现呢?还有,为什么腌制时列表上的五个值限制?任何帮助,将不胜感激。

最佳答案

您首先使用.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个整数的列表。

关于python - Python 3:Pickling和UnPickling类实例返回“无持久负载”错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19985818/

10-10 11:31