我想閱讀Test.xlsx
,但檔案位于另一個名為1
. 有沒有辦法指定檔案位置file_loc
?我試過這個,但似乎有一個錯誤。
import pandas as pd
import numpy as np
file_loc = "C:\Users\USER\OneDrive - Technion\Research_Technion\Python_PNM\Sept12_2022\1\Test.xlsx"
df = pd.read_excel(file_loc, index_col=None, na_values=['NA'], usecols="A,C:AA")
A=df["N"].to_numpy()
print([A])
錯誤是
line 11
file_loc = "C:\Users\USER\OneDrive - Technion\Research_Technion\Python_PNM\Sept12_2022\1\Test.xlsx"
^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape
uj5u.com熱心網友回復:
嘗試將路徑字串更改為原始字串
r'C:\Users\USER\OneDrive - Technion\Research_Technion\Python_PNM\Sept12_2022\1\Test.xlsx'
因為\1
轉換為表情符號(?
)。
或者\
用雙斜杠替換單斜杠\\
"C:\\Users\\USER\\OneDrive - Technion\\Research_Technion\\Python_PNM\\Sept12_2022\\1\\Test.xlsx"
import pandas as pd
import numpy as np
file_loc = r"C:\Users\USER\OneDrive - Technion\Research_Technion\Python_PNM\Sept12_2022\1\Test.xlsx"
df = pd.read_excel(file_loc, index_col=None, na_values=['NA'], usecols="A,C:AA")
A=df["N"].to_numpy()
print([A])
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/507391.html
上一篇:在Python中迭代檔案夾名稱
下一篇:列出np陣列中超出范圍的索引