Python异常(2)-(unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \...

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

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) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \...)