尝试使用 pickle.load() 时出现 UnicodeDecodeError

UnicodeDecodeError when trying to use pickle.load()

首先,我将这些信息转储到一个名为 'save0.pickle' 的文件中。

with open('save0.pickle', 'wb') as outfile:
    pickle.dump({
        'world_w': world.w,
        'world_h': world.h,
        'world_world': world.world,
        'player_icon': player.icon,
        'player_x': player.x,
        'player_y': player.y,
        'player_item': player.item,
    }, outfile)

这工作正常,但是当我尝试将文件的信息加载到变量中时出现错误,如下所示。

with open('save0.pickle', 'r') as infile:
    d = pickle.load(infile)

错误:UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 21: character maps to <undefined>

如有任何帮助,我们将不胜感激。

正如Keith所说,将open("save0.pickle", "r")更改为open("save0.pickle", "rb")解决了问题。