在Windows 7上从F:驱动器将.csv读取到 pandas 中 [英] Read a .csv into pandas from F: drive on Windows 7

查看:83
本文介绍了在Windows 7上从F:驱动器将.csv读取到 pandas 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读入熊猫并进行操作的Windows 7 64位F:驱动器上有一个.csv文件.

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

除了简单的文件名(例如'foo.csv')以外,我没有看到其他示例.

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)

错误消息显示:

IOError: Initializing from file failed

我在这里缺少一些简单的东西.有人可以看到吗?

I'm missing something simple here. Can anyone see it?

更新:

我确实获得了更多这样的信息:

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

我在阅读时遇到了权限错误.当我检查文件的属性时,我看到它是只读的.取消选中后,我能够成功读取892行.

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.

推荐答案

我不能保证这会起作用,但是值得一试:

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)

这篇关于在Windows 7上从F:驱动器将.csv读取到 pandas 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆