SyntaxError: (unicode error) ‘unicodeescape‘ codec can‘t decode bytes in position 2-3: truncated \UX

报错内容:
SyntaxError: (unicode error) ‘unicodeescape’ codec can’t decode bytes in position 2-3: truncated \UXXXXXXXX escape
在这里插入图片描述
解决办法:

原路径格式: df = pd.read_excel("C:\Users\XXXXXX\Desktop\测试数据\Dtest.xlsx")

路径书写错误,在Python中 \ 代表转义符,\u表示其后是UNICODE编码,因此\Users在这里会报错,需要在字符串前面加个 r(rawstring 原生字符串),可以避免python与正则表达式语法的冲突!
更改后的路径格式应该为:

df = pd.read_excel(r"C:\Users\XXXXXX\Desktop\测试数据\Dtest.xlsx")

猜你喜欢

转载自blog.csdn.net/blbyu/article/details/129252156