我试图创建一个函数,接受.csv数据文件的名称和表示该文件中的列标题的字符串列表,并返回一个dict对象,每个键是一个列标题,相应的值是一个numpy数组数据文件的该列中的值.
我的代码现在:
def columndata(filename, columns):
d = dict()
for col in columns:
with open(filename) as filein:
reader = csv.reader(filein)
for row in reader:
if col in row:
d.append(row)
return d
示例CSV如下所示:
test1,test2
3,2
1,5
6,47
1,4
列文件如下所示:
cols = ['test1', 'test2']
最终结果应该是这样的字典:
{'test1':[3,1,6,1], 'test2':[2, 5, 4, 4]}
解决方法:
您可以使用DictReader将CSV数据解析为dict:
import csv
from collections import defaultdict
def parse_csv_by_field(filename, fieldnames):
d = defaultdict(list)
with open(filename, newline='') as csvfile:
reader = csv.DictReader(csvfile, fieldnames)
next(reader) # remove header
for row in reader:
for field in fieldnames:
d[field].append(float(row[field])) # thanks to Paulo!
return dict(d)
print(parse_csv_by_field('a.csv', fieldnames=['cattle', 'cost']))