到目前为止,我的代码是一个基本上读取csv文件并打印它的内容的函数:
def read(filename):
with open(filename, 'r') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
for row in reader:
print(row)
sailor.csv
的内容:
name, mean performance , std dev
Alice, 100, 0,
Bob, 100, 5,
Clare, 100, 10,
Dennis, 90, 0,
Eva, 90, 5,
read('sailor.csv')
并运行函数
当前输出:
['name', ' mean performance ', ' std dev']
['Alice', ' 100', ' 0', '']
['Bob', ' 100', ' 5', '']
['Clare', ' 100', ' 10', '']
['Dennis', ' 90', ' 0', '']
['Eva', ' 90', ' 5', '']
必需的输出:
{'Dennis': (90.0, 0.0), 'Clare':(100.0, 10.0),
'Eva': (90.0, 5.0), 'Bob': (100.0, 5.0), 'Alice': (100.0, 0.0)}
我能如何实现这一输出?如果有帮助,请使用Python 3.4.2,我们将非常感谢您对答案的解释!
答案 0 :(得分:8)
使用csv
标准库和词典理解......
import csv
with open('sailor.csv') as csvfile:
reader = csv.reader(csvfile)
next(reader)
d = {r[0] : tuple(r[1:-1]) for r in reader}
d
将是您想要的词典。 d[1:-1]
将数组从第二个元素切割到第二个到最后一个元素。
编辑:跳过标题,转换为元组
答案 1 :(得分:2)
我认为这就是你想要的:
import csv
def read(filename):
out_dict = {}
with open(filename, 'r') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
next(csvfile) # skip the first row
for row in reader:
out_dict[row[0]] = float(row[1]), float(row[2])
print(row)
return out_dict
print(read('data.csv'))
打印:
{'Bob': (' 100', ' 5'), 'Clare': (' 100', ' 10'), 'Alice': (' 100', ' 0'), 'Dennis': (' 90', ' 0'), 'Eva': (' 90', ' 5')}
这里解释不多。只需将值放入字典中,并跳过添加的第一行。我认为人名是独一无二的。
答案 2 :(得分:2)
所以......我知道这个问题大部分都得到了解答,但我想我只是在混合中添加一个单行,以增加缩短答案:
from csv import reader
from itertools import islice
{r[0] : tuple(r[1:-1]) for r in islice(reader(open('sailor.csv')), 1, None)}
唯一真正新颖的事情是添加islice
以彻底跳过标题行。
答案 3 :(得分:0)
使用DictReader:
def read(filename):
with open(filename, 'r') as csvfile:
reader = csv.DictReader(csvfile, delimiter=',')
for row in reader:
print(row)
答案 4 :(得分:0)
如果可以的话,这是我的解决方案:
>>> import pyexcel as pe
>>> s = pe.load("sailor.csv", name_rows_by_column=0, name_columns_by_row=0)
>>> s.format(float)
>>> s
Sheet Name: csv
+--------+------------------+---------+---+
| | mean performance | std dev | |
+========+==================+=========+===+
| Alice | 100 | 0 | 0 |
+--------+------------------+---------+---+
| Bob | 100 | 5 | 0 |
+--------+------------------+---------+---+
| Clare | 100 | 10 | 0 |
+--------+------------------+---------+---+
| Dennis | 90 | 0 | 0 |
+--------+------------------+---------+---+
| Eva | 90 | 5 | 0 |
+--------+------------------+---------+---+
>>> del s.column[''] # delete the column which has '' as its name
>>> s.to_dict(True) # make a dictionary using row names as key
OrderedDict([('Alice', [100.0, 0.0]), ('Bob', [100.0, 5.0]),
('Clare', [100.0, 10.0]), ('Dennis', [90.0, 0.0]), ('Eva', [90.0, 5.0])])
的文档