从F: Windows 7的驱动器读取。csv到熊猫。

时间:2023-01-26 21:34:18

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位上驱动,我想读到熊猫和操作。

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.

现在熊猫也在工作。不需要移动文件或修改路径。谢谢你看。

4 个解决方案

#1


8  

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)

#2


2  

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文件使错误消失。

#3


2  

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

这是因为我之前的路径不正确。希望你能重新来过。

#4


0  

A better solution is to use literal strings like r'pathname\filename' rather than 'pathname\filename'. See Lexical Analysis for more details.

一个更好的解决方案是使用像r'pathname\filename'这样的文字字符串,而不是'pathname\filename'。更多细节请参见词汇分析。

#1


8  

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)

#2


2  

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文件使错误消失。

#3


2  

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

这是因为我之前的路径不正确。希望你能重新来过。

#4


0  

A better solution is to use literal strings like r'pathname\filename' rather than 'pathname\filename'. See Lexical Analysis for more details.

一个更好的解决方案是使用像r'pathname\filename'这样的文字字符串,而不是'pathname\filename'。更多细节请参见词汇分析。