2

ここに問題があります-私はピクルスにしようとしています、そして次にヒスコアをピクルスに外します。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

他に間違いがあったとしても心配しないでください。私はまだ学んでいますが、これを解決することはできません。

4

1 に答える 1

3

ファイルを反復処理すると、改行で区切られた行が得られます。これは、一連のピクルスを取得する方法ではありません。最初の行に部分的な 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
于 2011-08-04T19:38:58.607 に答える