美文网首页
Python异常(2)-(unicode error) '

Python异常(2)-(unicode error) '

作者: 橘猫吃不胖 | 来源:发表于2018-06-13 12:01 被阅读13次

早上写个了写文件的脚本,报错了

with open('C:\Users\yugui\OneDrive\python\odps_clean.txt','w',encoding='gbk') as file:
    file.write('\n'.join(origin_table_name))

一开始还以为是编码的问题,改了几次,发现不行,查了下,是转义字符的问题
''是转义字符,'\U'就变成unicode编码了,导致异常

我们可以直接加个r,表示对字符串不转义

with open(r'C:\Users\yugui\OneDrive\python\odps_clean.txt','w',encoding='gbk') as file:
    file.write('\n'.join(origin_table_name))

或者使用'/'表示,或者使用'\'来表示都行,记录下,总是忘记

相关文章

网友评论

      本文标题:Python异常(2)-(unicode error) '

      本文链接:https://www.haomeiwen.com/subject/wffjeftx.html