pickle.load()

当pickle.load(file)时,会直接报错:TypeError: file must have ‘read’ and ‘readline’ attributes;
当使用下面的这个代码运行时,

with open(file, 'rb') as f:  
    pickle.load(f)

会报错UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0x8b in position 6: ordinal not in range(128)。

解决方法1:替换代码中的pickle.load(f)为pickle.load(f, encoding=’bytes’)即可,在python3中需要加入encoding=’bytes’,而python2中不需要。

解决方法2:

with open(file, 'rb') as f:  
    pickle.loads(f.read())
Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐