Python读取文件时,出现SyntaxError: (unicode error) ‘unicodeescape‘ codec can‘t decode bytes in position 2-3

简短代码如下:
import pandas as pd
url = 'C:\Users\lic\Desktop\data\mp\1.csv'
df = pd.read_csv(url)
print(df)

运行错误:SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

解决办法:

1.在路径前面加r,保持原始值url = r'C:\Users\lic\Desktop\data\mp\1.csv'

2.替换双反斜杠url = 'C:\\Users\\lic\\Desktop\\data\\mp\\1.csv'

3.替换为正斜杠url = 'C:/Users/lic/Desktop/data/mp/1.csv'

知识点:Python中\为转义字符,如\n(换行)、\b(退格)、\t(横向制表符)等

上一篇:8-10 ObserveableCommand演示


下一篇:密码管理软件KeePassDX的模板的创建