python 保存scv文件乱码与报错的问题解决TypeError: a bytes-like object is required, not 'str'
2019-03-09 17:11
716 查看
Python版本: 3.6
今天准备对爬取下来的内容整理的时候, 想利用自带的csv模块处理保存到文件, 结果发现总是出现很多问题.
比如:
import csv f = open('/Users/neroasmar/Downloads/f.csv', 'wb') writer = csv.writer(f) writer.writerows(line) f.close()
这样会导致报错:
TypeError: a bytes-like object is required, not 'str'
import csv f = open('/Users/neroasmar/Downloads/f.csv', 'w') writer = csv.writer(f) writer.writerows(line) f.close()
这样直接会导致保存到文件内的中文文字发生乱码.
解决方法如下:
import csv import codecs # 使用codecs模块 f = codecs.open('/Users/neroasmar/Downloads/f.csv', 'w', 'utf_8_sig') writer = csv.writer(f) writer.writerows(data) f.close
这样就可以解决excel查看的时候中文乱码的问题, 而且还不会保错.
相关文章推荐
- python2.7代码在python3.5环境下TypeError: a bytes-like object is required, not 'str'问题
- python中Base64编码和解码:TypeError: a bytes-like object is required, not 'str'
- Python 3.x: TypeError: a bytes-like object is required, not 'str'
- Python 3.x Error:TypeError: a bytes-like object is required, not 'str'
- python3中,socket使用send函数时出现的错误:TypeError: a bytes-like object is required, not 'str'
- python 3.5: TypeError: a bytes-like object is required, not 'str' when writing to a file
- python3.5 模拟UDP客户端发送数据报文,报错:TypeError: a bytes-like object is required, not 'str'
- Python 3.5 Socket TypeError: a bytes-like object is required, not 'str' 错误提示
- Python中3+版本运行2+版本程序出现TypeError: a bytes-like object is required, not 'str'之读取PDF中的图片
- python write()参数错误,导致报错TypeError: a bytes-like object is required, not 'str'
- python socket send 函数 报错:TypeError: a bytes-like object is required, not 'str'
- Python学习中出现TpyeErrora bytes-like object is required, not 'str'
- python问题:TypeError: a bytes-like object is required, not 'str'
- TypeError: a bytes-like object is required, not 'str'
- 解决python3的pickle.load错误:a bytes-like object is required, not 'str'
- Python学习中出现TpyeErrora bytes-like object is required, not 'str'
- UDP通讯解决a bytes-like object is required, not 'str' 问题
- Python中“a bytes-like object is required, not 'str'”的问题
- 【Python】TypeError: a bytes-like object is required, not 'str'解决
- TypeError: a bytes-like object is required, not 'str'