Python基础 —— 使用 pickle 模块存储数据报错的解决办法
来源:互联网 发布:淘宝懒人模板 编辑:程序博客网 时间:2024/06/03 19:55
# 写入错误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
阅读全文
0 0
- Python基础 —— 使用 pickle 模块存储数据报错的解决办法
- python数据持久存储:pickle模块的基本使用
- python数据持久存储:pickle模块的基本使用
- python数据持久存储:pickle模块的基本使用
- python数据持久存储:pickle模块的基本使用
- python数据持久存储:pickle模块的基本使用
- python数据持久存储:pickle模块的基本使用
- python数据持久存储:pickle模块的基本使用
- python数据持久存储:pickle模块的基本使用
- Python数据存储:pickle模块的使用讲解
- python数据持久存储:pickle模块的基本使用
- python数据持久存储:pickle模块的基本使用
- python数据持久存储:pickle模块的基本使用
- python数据持久存储:pickle模块的基本使用
- python数据持久存储:pickle模块的基本使用
- python数据持久存储:pickle模块的基本使用
- python数据持久存储:pickle模块的基本使用
- python数据持久存储:pickle模块的基本使用
- HDU4973 A simple simulation problem
- 【二分图】poj 1469 COURSES
- 用于解决AlertDialog中需要向EditText输入内容却不能弹出输入法
- swift 中的标记关键字
- Etcd入门教程
- Python基础 —— 使用 pickle 模块存储数据报错的解决办法
- Ace of Aces
- 下拉刷新
- Dede开发
- 【转】浅谈深拷贝与浅拷贝
- 数据库优化02
- 03_模仿string
- Makefile 使用总结
- 椭圆的曲线模拟