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

时间:2018-12-26 02:30:54

标签: python python-3.x windows pickle

首先,我将此信息转储到名为“ 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>

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

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