Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中酸洗时使用EOFError_Python_Pickle_Eoferror - Fatal编程技术网

在Python中酸洗时使用EOFError

在Python中酸洗时使用EOFError,python,pickle,eoferror,Python,Pickle,Eoferror,问题出在这里——我正在尝试腌制,然后解开他的核心。当我使用pickle.load时,Python似乎认为我正在尝试加载一个名为“pickle”的文件。代码如下: def recieve_hiscores(): hiscores_file = open("hiscores_file.dat", "rb") for i in hiscores_file: hiscores = pickle.load(hiscores_file) hiscores =

问题出在这里——我正在尝试腌制,然后解开他的核心。当我使用pickle.load时,Python似乎认为我正在尝试加载一个名为“pickle”的文件。代码如下:

def recieve_hiscores():
    hiscores_file = open("hiscores_file.dat", "rb")
    for i in hiscores_file:
        hiscores = pickle.load(hiscores_file)
        hiscores = str(hiscores)
        print(hiscores)
以下是酸洗代码:

def send_hiscores(score):
    hiscores_file = open("hiscores_file.dat", "ab")
    pickle.dump(score, hiscores_file)
    hiscores_file.close()
下面是错误消息:

Traceback (most recent call last):
File "C:\Python31\My Updated Trivia Challenge.py", line 106, in <module>
main()
File "C:\Python31\My Updated Trivia Challenge.py", line 104, in main
recieve_hiscores()
File "C:\Python31\My Updated Trivia Challenge.py", line 56, in recieve_hiscores
hiscores = pickle.load(hiscores_file)
File "C:\Python31\lib\pickle.py", line 1365, in load
encoding=encoding, errors=errors).load()
EOFError
回溯(最近一次呼叫最后一次):
文件“C:\Python31\My Updated Trivia Challenge.py”,第106行,在
main()
文件“C:\Python31\My Updated Trivia Challenge.py”,第104行,主目录
接收他的核心()
文件“C:\Python31\My Updated Trivia Challenge.py”,第56行,在recieve\u hiscores中
hiscores=pickle.load(hiscores\u文件)
加载文件“C:\Python31\lib\pickle.py”,第1365行
编码=编码,错误=错误)。加载()
伊奥费罗

如果还有其他错误,请不要担心,我仍在学习,但我无法解决这个问题。

当您迭代文件时,会得到换行分隔的行。这不是你得到一系列泡菜的方式。引发文件结束错误,因为第一行有部分pickle

试试这个:

def recieve_hiscores():
    highscores = []
    with open("hiscores_file.dat", "rb") as hiscores_file:
        try:
            while True:
                hiscore = pickle.load(hiscores_file)
                hiscore = str(hiscore)
                print(hiscore)
                highscores.append(hiscore)
        except EOFError:
            pass
    return highscores

您的
hiscores\u文件.dat
不包含完整的Python对象。给我们看看你的酸洗代码和你在酸洗什么,也许我们可以看到问题。