编程语言
首页 > 编程语言> > Python读取文件时,出现SyntaxError: (unicode error) ‘unicodeescape‘ codec can‘t decode bytes in position 2-3

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(横向制表符)等

标签:unicodeescape,Users,lic,Python,SyntaxError,Desktop,url,csv,data
来源: https://blog.csdn.net/weixin_45112662/article/details/122805495