出现的原因:Windows和Linux下的文件编码不同,会在每行的末尾多出恶心的' ',解决方法,对字节码格式的每行' '替换为' '
- 替换代码
import pickle
# 替换
with open('./pet.pickle','rb') as f:
lines=f.readlines()
lines=[str(x, encoding="utf-8") .replace('
','
') for x in lines]
lines=[ bytes(x, encoding="utf8") for x in lines]
# 写入新的文件
with open('./washData/pet.pickle','wb') as nf:
for line in lines:
nf.write(line)
- 测试
import pickle
with open('./washData/pet.pickle','rb') as f:
p=pickle.load(f, encoding='latin1')
print(p)