首页 > 解决方案 > Pandas 找不到 .xlsx 文件。有没有办法让它工作?

问题描述

由于某些原因,python 无法加载我的数据,我正在尝试使用 pandas 从 excel 文件中加载数据。

kg = pd.read_excel('C:\Users\Desktop\NewData','Sales')

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

标签: pythonpython-3.xpandas

解决方案


由于转义参数,python 试图将路径中的 '\U' 理解为 unicode 字符。为避免这种情况,请尝试在字符串之前添加 r ,这意味着 raw 以停止字符串格式化。或者只对每个 '\' 使用 '\',因为字符串格式需要两个反斜杠来检索一个反斜杠。前任:

kg = pd.read_excel(r'C:\Users\Desktop\NewData','Sales')

或者

kg = pd.read_excel('C:\\Users\\Desktop\\NewData','Sales')

推荐阅读