解决python3读取Python2存储的pickle文件问题

yipeiwu_com7年前Python基础

我在使用python3.5处理一个序列化文件xxx.pk,不过这个.pk文件是我在python2.7里面存储的,当我用python3读取的时候就会报如下的错误。

import pickle
picklefile=open('2ohsumed_wmd_d.pk','rb')
data=pickle.load(picklefile)
 
print (data)

UnicodeDecodeError: 'ascii' codec can't decode byte 0xa0 in position 11: ordinal not in range(128)

解决办法:

import pickle
picklefile=open('2ohsumed_wmd_d.pk','rb')
data=pickle.load(picklefile,encoding='iso-8859-1')
 
print (data)

以上这篇解决python3读取Python2存储的pickle文件问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python读取目录下最新的文件夹方法

如下所示: def new_report(test_report): lists = os.listdir(test_report) # 列出目录的下所有文件和文件...

Python线上环境使用日志的及配置文件

目录 瞎比比 与 print 相比 logging 有什么优势? 基础用法 保存到文件 多模块使用 logging 使用配置文件配置 logging 瞎...

Python中函数的参数传递与可变长参数介绍

Python中函数的参数传递与可变长参数介绍

1.Python中也有像C++一样的默认缺省函数 复制代码 代码如下: def foo(text,num=0):     print text,num fo...

提升Python效率之使用循环机制代替递归函数

斐波那契数列 当年,典型的递归题目,斐波那契数列还记得吗? def fib(n): if n==1 or n==2: return 1 else: retur...

Python使用filetype精确判断文件类型

filetype.py Small and dependency free Python package to infer file type and MIME type checkin...