简体   繁体   中英

Reading excel file with pandas in python how to fix : FileNotFoundError(2, 'No such file or directory')

My ultimate goal is to get the data from two columns so I can plot and filter it, so I am trying to convert that data into an array. So I am trying to import data from two columns in an excel spreadsheet but pandas won't find the file.

 #C:\Users\curti is my cwd
 df = pd.read_excel('\Desktop\My Undergrad Thesis\Raw Data Raw Nitrogen\Compiled Data - Raw (Nitrogen).xlsm', sheetname='2018_10_22_Test6') 
 df.head()

 print('success')

I'm sure it is something obvious but I have been looking online for a while and nothing has fixed it, so if someone could point me in the right direction that would be great! Also if there's a better way to achieve my goal please let me know.

The issue here is related to how Python reads strings and therefore would affect file inputs.

\\ in Python is a special character also known as an escape character in representing other special characters such as \\n or \\t . For example \\n returns the newline character. To actually print a backslash you will need to use \\\\ .

'\\Desktop\\My Undergrad Thesis\\Raw Data Raw Nitrogen\\Compiled Data - Raw (Nitrogen).xlsm'

or you can also use the 'r' literal which is described in the Python documentation:

Both string and bytes literals may optionally be prefixed with a letter 'r' or 'R'; such strings are called raw strings and treat backslashes as literal characters. As a result, in string literals, '\\U' and '\\u\u0026#39; escapes in raw strings are not treated specially. Given that Python 2.x's raw unicode literals behave differently than Python 3.x's the 'ur' syntax is not supported.

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