I am trying to use pandas read_excel to create a dataframe. In the past, I have used it many times without difficulties. On another machine, I am suddenly not able to do it. I have tried:
import pandas as pd
import os
os.getcwd() -- 'C:\\Users\\User'
df = pd.read_excel (r"C:/Users/User/TestSheet.xlsx")
df = pd.read_excel (r"C://Users//User//TestSheet.xlsx")
df = pd.read_excel (r"C:\Users\User\TestSheet.xlsx")
df = pd.read_excel (r"C:\\Users\\User\\TestSheet.xlsx")
df = pd.read_excel ("C:/Users/User/TestSheet.xlsx")
df = pd.read_excel ("C://Users//User//TestSheet.xlsx")
df = pd.read_excel ("C:\Users\User\TestSheet.xlsx")
df = pd.read_excel ("C:\\Users\\User\\TestSheet.xlsx")
df = pd.read_excel ("TestSheet.xlsx")
df = pd.read_excel ('TestSheet.xlsx')
To sum up, I used both wd and path name, I used both single and double quotes, back slashes and forward slashes. I have tried quite a few things. This should be so easy.
Mostly I get the file not found. In one occasion, this shows up:
df = pd.read_excel ("C:\\Users\User\\TestSheet.xlsx")
^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 9-10: truncated \UXXXXXXXX escape
Any other suggestions as to why this is not working?
Thanks, KR
~ M
Not sure this is the issue, but have you notice you use both slash and backslash? This may cause discrepancies in different operating systems.
Try to read file in the following manner:
import os
import pandas as pd
df = pd.read_excel(os.path.join("C:", "Users", "User", "TestSheet.xlsx"))
or
import os
import pandas as pd
directory_path = os.getcwd()
df = pd.read_excel(os.path.join(directory_path, "TestSheet.xlsx"))
This should be cross platform compatible
Read the sample data
1 Using Google sheet like colab
2 Without pandas read excel function
Check the Code
import pandas as pd
import os
p = os.path.dirname(os.path.realpath("C:\Car_sales.xlsx"))
name = 'C:\Car_sales.xlsx'
path = os.path.join(p, name)
Z = pd.read_excel(path)
Z.head()
Still not able to use the read_excel properly, I used the read_clipboard function. Odd thing is that I can actually write multiple Excel files from my dataframe.
For now, this is what I need.
Thanks for your input.
KR ~M
Edit:
I actually found the issue. The file was named Testsheet.xlsx (under the hood, it actually reads Testsheet.xlsx.xlsx. I renamed the file to Testsheet and everything worked.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.