Python 'CSV 不存在' - Pandas DataFrame
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17661136/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'CSV does not exist' - Pandas DataFrame
提问by Woody Pride
I'm having difficulty reading a csv file into the pandas data frame. I am a total newcomer to pandas, and this is preventing me from progressing. I have read the documentation and searched for solutions, but I am unable to proceed. I have tried the following to no avail...
我无法将 csv 文件读入 pandas 数据框中。我是大熊猫的新手,这阻碍了我的进步。我已阅读文档并搜索了解决方案,但我无法继续。我尝试了以下方法无济于事...
import pandas as pd
import numpy as np
pd.read_csv('C:\Users\rcreedon\Desktop\TEST.csv')
pd.read_csv("C:\Users\rcreedon\Desktop\TEST.csv")
and similar permutations with/without quotation marks.
以及带/不带引号的类似排列。
It spits out a large composite error that ends in:
它吐出一个大的复合错误,结尾为:
IOError: File C:\Users
creedon\Desktop\TEST.csv does not exist
It seems strange that in the error it misses of the "r" from "rcreedon". Is this what's causing the problem?
奇怪的是,在错误中它错过了“rcreedon”中的“r”。这是导致问题的原因吗?
Just for the sake of it i also tried
只是为了它我也试过
pd.read_csv('C:\rcreedon\Desktop\TEST.csv')
And again the 'r' was missed when the error was returned.
返回错误时再次错过了“r”。
Sorry to be such a block head, but I'm struggling here....
很抱歉成为这样一个块头,但我在这里挣扎......
Any help appreciated.
任何帮助表示赞赏。
采纳答案by Dr. Jan-Philip Gehrcke
"\r" usually is interpreted as a special character and means carriage return. Either add a 'r' prefix to your string literals which prevents this special sequence from being interpreted (e.g. path = r"foo\rar"
), or, as already suggested, just use a normal slash as path delimiter. Python is intelligent enough for it to also work on Windows :-)
“\r”通常被解释为一个特殊字符,表示回车。要么在您的字符串文字中添加一个 'r' 前缀,以防止解释这个特殊序列(例如path = r"foo\rar"
),或者,正如已经建议的那样,只使用普通斜杠作为路径分隔符。Python 足够智能,可以在 Windows 上运行 :-)
回答by BrenBarn
Just use a raw string:
只需使用原始字符串:
pd.read_csv(r'C:\Users\rcreedon\Desktop\TEST.csv')
回答by xtian_prz
I had a similar problem. You may need to check and see how many tabs are in your excel file. I had a problem where Excel 2010 would not save the entire workbook as a csv file and I had to save each tab individually. After that I was able to open using Pandas. I would suggest using r":C\pathname..." too.
我有一个类似的问题。您可能需要检查并查看您的 excel 文件中有多少个选项卡。我遇到了一个问题,即 Excel 2010 无法将整个工作簿保存为 csv 文件,我不得不单独保存每个选项卡。之后我就可以使用 Pandas 打开了。我建议也使用 r":C\pathname..." 。