如何将CSV文件放入数组中?

时间:2014-12-05 15:09:58

标签: python arrays csv multidimensional-array python-2.x

我正在读取一个CSV文件,我想把它放在一个数组中,这样我就可以根据行索引快速查找信息。这是我得到的,但似乎行不能使用split()。有什么建议吗?

print csvFilePath
a = []

reader = csv.reader(open(csvFilePath,'rU'),dialect=csv.excel_tab)
print reader
for row in reader:
    print row
    a.append(row.split(','))

print a[45]['firstname']

3 个答案:

答案 0 :(得分:2)

你最需要的是DictReader(正如布鲁诺指出的那样。他更快地拉动扳机。)。它接受文件名并将每行作为字典返回,这是您想要的。这将使您的代码:

import csv
a = []

reader = csv.DictReader(open("so.csv",'rU'), dialect=csv.excel_tab, delimiter=',')
print reader
for row in reader:
    print row
    a.append(row)

print a[2]['Make']

可选择简化为:

import csv
with open("so.csv",'rU') as f:
    a = list(csv.DictReader(f, dialect=csv.excel_tab, delimiter=','))
print a[2]['Make']

使用一些样本数据(来自Wikipedia):

Year,Make,Model,Description,Price
1997,Ford,E350,"ac, abs, moon",3000.00
1999,Chevy,"Venture ""Extended Edition""","",4900.00
1999,Chevy,"Venture ""Extended Edition, Very Large""",,5000.00
1996,Jeep,Grand Cherokee,"MUST SELL!
air, moon roof, loaded",4799.00

打印:

<csv.DictReader instance at 0x7fe7a3aedfc8>
{'Price': '3000.00', 'Description': 'ac, abs, moon', 'Make': 'Ford', 'Model': 'E350', 'Year': '1997'}
{'Price': '4900.00', 'Description': '', 'Make': 'Chevy', 'Model': 'Venture "Extended Edition"', 'Year': '1999'}
{'Price': '5000.00', 'Description': '', 'Make': 'Chevy', 'Model': 'Venture "Extended Edition, Very Large"', 'Year': '1999'}
{'Price': '4799.00', 'Description': 'MUST SELL!\nair, moon roof, loaded', 'Make': 'Jeep', 'Model': 'Grand Cherokee', 'Year': '1996'}
Chevy

这假定您的文件以Year,Make,Model,Description,Price之类的标头开头,以提供密钥。如果没有,您可以将标题作为列表传递给DictReader

reader = csv.DictReader(open("so.csv",'rU'), ["Year", "Make", "Model", "Description", "Price"], dialect=csv.excel_tab, delimiter=',')

另请注意,吐痰char由delimiter=','参数给出。

答案 1 :(得分:0)

嗯,csv模块的重点是避免自己解析csv文件,所以不,row“不能使用拆分”,因为它已经(正确地)“拆分”成列表。如果你想要一个行列表,那就像

一样简单
with  open(csvFilePath,'rU') as f:
    reader = csv.reader(f), dialect=csv.excel_tab)
    a = list(reader)

现在,如果您需要一个dicts列表(这样您可以使用a[45]['firstname']),则必须使用csv.DictReader()代替(https://docs.python.org/2/library/csv.html#csv.DictReader),或者从中构建dict行和标题列表,即:

headers = ["firstname", "lastname", "has_parrot",]
with  open(csvFilePath,'rU') as f:
    reader = csv.reader(f), dialect=csv.excel_tab)
    a = [dict(zip(headers, row)) for row in reader]

但真正使用csv.DictReader是更好的选择。

答案 2 :(得分:0)

Python默认支持两种类型的csv文件,一种是这样的,逗号分隔:

1,2,3

另一个就像,标签分开:

1    2    3

 1\t2\t3

现在假设你有这些原始数据:

 firstname,surname,..
 Adam,Smith,...
 ...

现在根据您的类型,使用我的迷你包装库来获取您想要的二维数据:

 >>> import pyexcel as pe
 >>> sheet = pe.load("your_file.csv", name_columns_by_row=0) # or "your_file.tsv"
 >>> records = sheet.to_records()
 >>> records[45]["firstname"]

详细文档为here