简体   繁体   中英

How do I read a XLSX if the filepath or filename has an apostrophe?

I am trying to read in a XLSX file, but my filepath has an apostrophe. Are there any workarounds? Had to re-write, have a new error, see below:

df_siteData = pd.DataFrame()
df_siteData = pd.read_excel('C:\Users\Ben.Dreyfuss\Documents\milly's\milly's- raw data.xlsx')

Error: File "<ipython-input-61-215e07fcbd29>", line 2
df_siteData = pd.read_excel('C:\Users\Ben.Dreyfuss\Documents\milly's\milly's- raw data.xlsx')
                                                                   ^
 SyntaxError: invalid syntax

The problem isn't with the apostrophe, it's with the backslash ( \\ ) character. It is being used simultaneously as a directory separator and as an escape character.

Try this:

pd.read_excel(r"C:\Users\Ben.Dreyfuss\Documents\milly's\milly's- raw data.xlsx")

It will either work or give you a different error.

Alternatively, you can take advantage of a Windows feature -- either flavor of slash works equally well as a directory separator. The following two are equivalent:

pd.read_excel("C:/Users/Ben.Dreyfuss/Documents/milly's/milly's- raw data.xlsx")

or

pd.read_excel('C:/Users/Ben.Dreyfuss/Documents/milly\'s/milly\'s- raw data.xlsx')

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM