gpt4 book ai didi

python - 在 Python 中 pickle 时出现 EOFError

转载 作者:太空宇宙 更新时间:2023-11-04 01:39:04 24 4
gpt4 key购买 nike

这就是问题所在 - 我正在尝试 pickle ,然后再取消 pickle 他的核心。当我使用 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)

这是 pickle 代码:

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

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

最佳答案

当您遍历文件时,您会得到换行符分隔的行。这不是您获得一系列 pickle 的方式。出现文件结尾错误是因为第一行有部分 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

关于python - 在 Python 中 pickle 时出现 EOFError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6947220/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com