csv Pandas中的Excel数据引发FileNotFoundError

hpcdzsge  于 2023-05-04  发布在  其他
关注(0)|答案(1)|浏览(168)
import pandas as pd

# Get stock data from excel
file_path = pd.read_csv("C:\Users\OneDrive\Desktop\nifty1.csv")

#show the data
df = pd.read_csv(file_path)

print(df.head())

我得到的代码错误是

File "<ipython-input-59-276db8347c3f>", line 4
    file_path = pd.read_csv("C:\Users\OneDrive\Desktop\nifty1.csv")
                                                                  ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
yqhsw0fo

yqhsw0fo1#

file_path = pd.read_csv("C:\Users\OneDrive\Desktop\nifty1.csv")

字符串"\U"在Python中有特殊的含义。
或者通过在前面放置r将其更改为原始字符串:

r"C:\Users\OneDrive\Desktop\nifty1.csv"

或者使用正斜杠代替反斜杠:

"C:/Users/OneDrive/Desktop/nifty1.csv"

相关问题