- python 在Linux上执行,版本是2.7
- python中的文件操作,写一个换行符,出现了如下错误:
Traceback (most recent call last):
File "parse_new.py", line 130, in <module>
main()
File "parse_new.py", line 102, in main
f.write("\n")
TypeError: must be unicode, not str
Traceback (most recent call last):
File "parse_new.py", line 130, in <module>
main()
File "parse_new.py", line 102, in main
f.write("\n")
TypeError: must be unicode, not str
python 2.7 没有默认Unicode编码,所以需要自己把元素进行编码
with io.open('xxxx', 'a') as f:
for i in LIST:
f.write(i.decode('utf-8','ignore'))
f.write(u"\n")
ps:
我在本地的python是3以上,本地运行的代码没有问题,但是到开发机上就出现问题,很无语啊,应该在开发机上也安装一个python3,这是紧要的
4 回答4.5k 阅读✓ 已解决
1 回答3.4k 阅读✓ 已解决
4 回答3.8k 阅读✓ 已解决
3 回答2.2k 阅读✓ 已解决
1 回答4.5k 阅读✓ 已解决
1 回答3.9k 阅读✓ 已解决
2 回答428 阅读✓ 已解决
试试这个