Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
588 views
in Technique[技术] by (71.8m points)

python - Read a .csv into pandas from F: drive on Windows 7

I have a .csv file on my F: drive on Windows 7 64-bit that I'd like to read into pandas and manipulate.

None of the examples I see read from anything other than a simple file name (e.g. '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.

Now pandas is working as well. No need to move the file or amend the path. Thanks for looking.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...