python提示错误TypeError: write() argument must be str, not bytes
来源:互联网 发布:917发卡源码 编辑:程序博客网 时间:2024/05/21 23:31
还是在学习《Machine Learning in Action》中决策树一章中,在学习决策树存储过程中先是提示错误TypeError: write() argument must be str, not bytes,看错误信息是说write()函数的参数必须为str,而不是字节bytes。不明白,看源码哪里提示错误了,pickle.dump(inputTree,fw)这一条语句编译时出错了,那么我们先看下pickle.dump()的用法
从倒数第二句可以看出,我们的问题出在fw这个变量上,也就是fw = open(filename,'w')这条语句有问题。也就是说fw这个变量的类型应该是bytes的,所以可以将'w'改为'wb',修改后再编译成功,同样的原理,提示UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 0: illegal multibyte sequence错误时,是由于之前保存的txt文件是用二进制写入的所以在读取时也应该用二进制,即读文件中的fr = open(filename)应该改为fr = open(filename, 'rb')。OK,问题解决!测试下:
成功!
阅读全文
1 0
- python提示错误TypeError: write() argument must be str, not bytes
- Python 读写文件 中文乱码 错误TypeError: write() argument must be str, not bytes+
- Python 读写文件 错误TypeError: write() argument must be str, not bytes+
- TypeError: write() argument must be str, not bytes
- docx2txt TypeError: write() argument must be str, not bytes
- write() argument must be str, not bytes
- python-TypeError: write() argument must be str, not numpy.float64
- Python出现TypeError: file() argument 1 must be encoded string without NULL bytes, not str问题解决
- 使用pickle将对象存储到文件中时出现 TypeError: write() argument must be str, not bytes
- Python中报错TypeError: must be str, not bytes
- TypeError: must be str, not bytes
- TypeError: must be str, not bytes
- python3-网络编程-TypeError: send() argument 1 must be bytes or buffer, not str异常
- 关于TypeError: strptime() argument 1 must be str, not bytes解析
- 保存网页TypeError: must be str, not bytes
- TypeError: the JSON object must be str, not 'bytes'
- Python之Headers value 1 must be of type str or bytes, not <class 'int'>错误的解决
- can't use a string pattern on a bytes-like object错误和must be str, not bytes错误
- Linux下通过终端安装mysql
- swiper+图片自动+无限轮播
- 关于获取Gridcontrol中repositoryItemLookUpEdit的使用
- 选择排序
- python多进程插入redis
- python提示错误TypeError: write() argument must be str, not bytes
- ubuntu部署flask
- 机器学习-特征提取
- 基于Dragonboard410c的智能音箱(三)
- 解决combox在火狐下不支持中文检索问题
- angularjs过滤器
- eclise 部署web工程报 There are no resources that can be added or removed from the server.
- html中table表格内容水平垂直居中显示
- char与varchar类型区别的深度剖析