简体   繁体   English

在python中用熊猫读取Excel文件如何修复:FileNotFoundError(2,'No such file or directory')

[英]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. 因此,我尝试从excel电子表格的两列中导入数据,但pandas找不到该文件。

 #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. 这里的问题与Python如何读取字符串有关,因此会影响文件输入。

\\ in Python is a special character also known as an escape character in representing other special characters such as \\n or \\t . Python中的\\是一个特殊字符,在表示其他特殊字符(例如\\n\\t也称为转义字符。 For example \\n returns the newline character. 例如, \\n返回换行符。 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: 或者您也可以使用Python文档中描述的'r'文字:

Both string and bytes literals may optionally be prefixed with a letter 'r' or 'R'; 字符串和字节文字都可以选择以字母“ r”或“ 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. 结果,在字符串文字中,原始字符串中的'\\ U'和'\\ u'转义符未得到特殊处理。 Given that Python 2.x's raw unicode literals behave differently than Python 3.x's the 'ur' syntax is not supported. 鉴于Python 2.x的原始unicode文字的行为与Python 3.x的不同,因此不支持'ur'语法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python 3:如何修复FileNotFoundError:[Errno 2]没有这样的文件或目录 - Python 3: How to fix FileNotFoundError: [Errno 2] No such file or directory Mac 操作系统; 蟒蛇3; FileNotFoundError: [Errno 2] 没有这样的文件或目录:''。 如何解决? - Mac OS; Python3; FileNotFoundError: [Errno 2] No such file or directory: ''. How to fix it? FileNotFoundError: [Errno 2] 没有这样的文件或目录; 怎么修? - FileNotFoundError: [Errno 2] No such file or directory; How to fix? 如何解决:FileNotFoundError:[Errno 2]没有这样的文件或目录 - How to fix: FileNotFoundError: [Errno 2] No such file or directory 如何修复FileNotFoundError Errno 2没有这样的文件或目录 - how to fix FileNotFoundError Errno 2 no such file or directory 如何修复 FileNotFoundError: [Errno 2] No such file or directory: no - How to fix FileNotFoundError: [Errno 2] No such file or directory: no 如何解决 FileNotFoundError: [Errno 2] No such file or directory: in Pandas? - How to resolve FileNotFoundError: [Errno 2] No such file or directory: in Pandas? FileNotFoundError: [Errno 2] 没有这样的文件或目录。 Python 无法正确读取文件 - FileNotFoundError: [Errno 2] No such file or directory. Python not reading files correctly python:FileNotFoundError:[Errno 2]没有这样的文件或目录 - python: FileNotFoundError: [Errno 2] No such file or directory Python FileNotFoundError:[错误2]没有这样的文件或目录 - Python FileNotFoundError: [Errno 2] No such file or directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM