Python 在 Windows 7 上从 F: 驱动器将 .csv 读入 Pandas
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16952632/
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
Read a .csv into pandas from F: drive on Windows 7
提问by duffymo
I have a .csv file on my F: drive on Windows 7 64-bit that I'd like to read into pandas and manipulate.
我的 F: 驱动器上有一个 .csv 文件,在 Windows 7 64 位上,我想读入 Pandas 并进行操作。
None of the examples I see read from anything other than a simple file name (e.g. 'foo.csv').
我看到的所有示例都不是从简单的文件名(例如'foo.csv')读取的。
When I try this I get error messages that aren't making the problem clear to me:
当我尝试这个时,我收到错误消息,但没有让我清楚问题:
import pandas as pd
trainFile = "F:/Projects/Python/coursera/intro-to-data-science/kaggle/data/train.csv"
trainData = pd.read_csv(trainFile)
The error message says:
错误消息说:
IOError: Initializing from file failed
I'm missing something simple here. Can anyone see it?
我在这里遗漏了一些简单的东西。任何人都可以看到吗?
Update:
更新:
I did get more information like this:
我确实得到了更多这样的信息:
import csv
if __name__ == '__main__':
trainPath = 'F:/Projects/Python/coursera/intro-to-data-science/kaggle/data/train.csv'
trainData = []
with open(trainPath, 'r') as trainCsv:
trainReader = csv.reader(trainCsv, delimiter=',', quotechar='"')
for row in trainReader:
trainData.append(row)
print trainData
I got a permission error on read. When I checked the properties of the file, I saw that it was read-only. I was able to read 892 lines successfully after unchecking it.
我在读取时遇到权限错误。当我检查文件的属性时,我看到它是只读的。取消选中后,我能够成功读取 892 行。
Now pandas is working as well. No need to move the file or amend the path. Thanks for looking.
现在大熊猫也在工作。无需移动文件或修改路径。谢谢你看。
采纳答案by zwol
I cannot promise that this will work, but it's worth a shot:
我不能保证这会起作用,但值得一试:
import pandas as pd
import os
trainFile = "F:/Projects/Python/coursera/intro-to-data-science/kaggle/data/train.csv"
pwd = os.getcwd()
os.chdir(os.path.dirname(trainFile))
trainData = pd.read_csv(os.path.basename(trainFile))
os.chdir(pwd)
回答by Hanan Shteingart
A better solution is to use literal strings like r'pathname\filename' rather than 'pathname\filename'. See Lexical Analysisfor more details.
更好的解决方案是使用像 r'pathname\filename' 这样的文字字符串而不是 'pathname\filename'。有关更多详细信息,请参阅词法分析。
回答by Andrew Sturges
If you're sure the path is correct, make sure no other programs have the file open. I got that error once, and closing the Excel file made the error go away.
如果您确定路径正确,请确保没有其他程序打开该文件。我遇到过一次该错误,关闭 Excel 文件使错误消失。
回答by user3126530
I also got the same issue and got that resolved .
我也遇到了同样的问题并解决了。
Check your path for the file correctly
正确检查文件的路径
I initially had the path like
我最初有这样的路径
dfTrain = pd.read_csv("D:\Kaggle\labeledTrainData.tsv",header=0,delimiter="\t",quoting=3)
This returned an error because the path was wrong .Then I have changed the path as below.This is working fine.
这返回了一个错误,因为路径错误。然后我更改了路径,如下所示。这工作正常。
dfTrain = dfTrain = pd.read_csv("D:\Kaggle\labeledTrainData.tsv\labeledTrainData.tsv",header=0,delimiter="\t",quoting=3)
This is because my earlier path was not correct.Hope you get it reolved
这是因为我之前的路径不正确。希望你能解决
回答by sheldonzy
This happens to me quite often. Usually I open the csv file in Excel, and save it as an xlsx file, and it works.
这经常发生在我身上。通常我在 Excel 中打开 csv 文件,并将其另存为 xlsx 文件,它可以工作。
so instead of
所以而不是
df = pd.read_csv(r"...\file.csv")
Use:
用:
df = pd.read_excel(r"...\file.xlsx")

