您的位置:首页 > 编程语言 > Python开发

Python在写入文件时中文报错IOError: [Errno 22] invalid mode ('w') or filename

2018-01-31 10:40 691 查看
Python版本为2.7.0,IDE为PyCharm

f = open(filename, 'w')
f.write(html)


filename中包含中文,html为一个网页数据,结果报了如下的错

File "D:/PythonWorkSpace/test.py", line 42, in <module>
tiebaSpider(fullUrl,beginPage,endPage)
File "D:/PythonWorkSpace/test.py", line 31, in tiebaSpider
writeFile(html,filename)
File "D:/PythonWorkSpace/test.py", line 20, in writeFile
f = open(filename, 'w')
IOError: [Errno 22] invalid mode ('w') or filename: '\xe7\xac\xac1\xe9\xa1\xb5.html'

将代码修改为:


f = open(filename.decode('utf-8'), 'w')
f.write(html)


成功运行,因为是Python中的字符串的大概分为为str和Unicode两种形式,其中str常用的编码类型为utf-8,gb2312,gbk等等,Python使用Unicode作为编码的基础类型,open(filename, ‘w’)这个方法中,filename这个参数必须是Unicode编码的参数。

参考资料:https://www.cnblogs.com/dragonisnotghost/p/4515581.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐