[英]Converting the contents of a CSV file into a dictionary
The code I have so far is in a function that basically reads a csv file and prints it's contents: 到目前为止我的代码是一个基本上读取csv文件并打印其内容的函数:
def read(filename):
with open(filename, 'r') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
for row in reader:
print(row)
Contents of sailor.csv
: 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')
and running the function read('sailor.csv')
并运行该函数
current output: 电流输出:
['name', ' mean performance ', ' std dev']
['Alice', ' 100', ' 0', '']
['Bob', ' 100', ' 5', '']
['Clare', ' 100', ' 10', '']
['Dennis', ' 90', ' 0', '']
['Eva', ' 90', ' 5', '']
required output: 所需产量:
{'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)}
any ideas how I can achieve that output? 任何想法如何实现这一输出? Using Python 3.4.2 if that helps, explanation of your answer will be appreciated! 如果有帮助,请使用Python 3.4.2,我们将非常感谢您对答案的解释!
using the csv
standard library and a dictionary comprehension... 使用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}
Where d
will be the dictionary you want. 其中d
将是你想要的字典。 d[1:-1]
slices the array from the second to the second to last element. d[1:-1]
将数组从第二个切换到第二个到最后一个元素。
EDIT: skips headers, converts to tuples 编辑:跳过标题,转换为元组
I think this is what you want: 我想这就是你想要的:
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'))
Prints: 打印:
{'Bob': (' 100', ' 5'), 'Clare': (' 100', ' 10'), 'Alice': (' 100', ' 0'), 'Dennis': (' 90', ' 0'), 'Eva': (' 90', ' 5')}
Not to much to explain here. 这里解释不多。 Just putting the values in the dictionary, and skipping the first row added. 只需将值放入字典中,并跳过添加的第一行。 I assumed that the persons names are unique. 我认为人名是独一无二的。
So... I know this question has mostly been answered, but I thought I'd just throw a one-liner in the mix to add on to the shortening answers: 所以...我知道这个问题已经得到了回答,但我认为我只是在混合中添加一个单行来增加缩短答案:
from csv import reader
from itertools import islice
{r[0] : tuple(r[1:-1]) for r in islice(reader(open('sailor.csv')), 1, None)}
The only really novel thing is adding islice
to skip the header row cleanly. 唯一真正新颖的事情是添加islice
以干净地跳过标题行。
Use DictReader: 使用DictReader:
def read(filename):
with open(filename, 'r') as csvfile:
reader = csv.DictReader(csvfile, delimiter=',')
for row in reader:
print(row)
Here is my solution if I may: 如果我可以,这是我的解决方案:
>>> 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])])
Here is the documentation on pe.load and to_dict of pyexcel 这是关于pyexcel的pe.load和to_dict的文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.