Python 中酸洗时出现 EOFError

发布于 2024-11-28 00:04:12 字数 1016 浏览 2 评论 0原文

问题是 - 我正在尝试腌制,然后解开他的核心。当我使用 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

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

Here's the problem - I'm trying to pickle, and then unpickle hiscores. When I use pickle.load, Python seems to think that I'm trying to load a file called 'Pickle' that I have. Here's the code:

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)

Here's the pickling code:

def send_hiscores(score):
    hiscores_file = open("hiscores_file.dat", "ab")
    pickle.dump(score, hiscores_file)
    hiscores_file.close()

And here's the error message:

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

Don't worry if there's any other mistakes, I'm still learning, but I can't work this out.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ぶ宁プ宁ぶ 2024-12-05 00:04:12

当您迭代该文件时,您会得到换行符分隔的行。这不是你得到一系列泡菜的方式。由于第一行包含部分 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

When you iterate over the file, you get newline separated lines. This is NOT how you get a series of pickles. The end of file error is raised because the first line has a partial pickle.

Try this:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文