热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

python3文件读写,编码错误UnicodeDecodeError

问题:python3withopen文件进行读写,报编码错误usrlocalCellarpython33.5.2FrameworksPython.framewo
问题:python3 with open文件进行读写,报编码错误
/usr/local/Cellar/python3/3.5.2/Frameworks/Python.framework/Versions/3.5/lib/python3.5/encodings/ascii.py, line 26, in decode
return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: \'ascii\' codec can\'t decode byte 0xe7 in position 478: ordinal not in range(128)
 
代码如下:
with open("../testsuit/temptestcaseslist.txt","w") as f:
    f.write(content)
 
解决方法:
with open()中增加参数【encoding=‘utf-8’】指定读写文件的编码格式
 
with open("../testsuit/temptestcaseslist.txt","w",encoding='utf-8') as f:
    f.write(content)

 

 

推荐阅读
author-avatar
手机用户2502856985
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有