当前位置:主页 > 软件编程 > Python代码 >

Python使用pickle模块存储数据报错解决示例代码

时间:2021-06-17 09:16:34 | 栏目:Python代码 | 点击:

本文研究的主要是Python使用pickle模块存储数据报错解决方法,以代码的形式展示,具体如下。

首先来了解下pickle模块

接下来我们看下Python使用pickle模块存储数据报错解决方法。

代码:

# 写入错误
TypeError: write() argument must be str, not bytes


# 读取错误
UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 0: illegal multibyte sequence

解决方案:

def storeTree(inputTree, fielname):
  import pickle
  # 写文件时,注明 'wb'
  fw = open(fielname, 'wb')
  pickle.dump(inputTree, fw)
  fw.close()

def grabTree(filename):
  import pickle
   # 读文件时,注明 'rb'
  fr = open(filename, 'rb')
  fr = open(filename)
  return pickle.load(fr)

storeTree(myTree, 'classifierStorage.txt')
print(grabTree('classifierStorage.txt'))

输出:

{'no surfacing': {0: 'no', 1: {'flippers': {0: 'no', 1: 'yes'}}}}

Process finished with exit code 0

总结

以上就是本文关于Python使用pickle模块存储数据报错解决示例代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

您可能感兴趣的文章:

相关文章