[英]Reading csv file in Python and create a dictionary
I am trying to read a csv file in python 27 to create a dictionary. 我试图在python 27中读取一个csv文件来创建一个字典。 CSV file looks like- CSV文件看起来像 -
SI1440269,SI1320943,SI1321085 SI1440270,SI1320943,SI1321085,SI1320739 SI1440271,SI1320943 SI1440269,SI1320943,SI1321085 SI1440270,SI1320943,SI1321085,SI1320739 SI1440271,SI1320943
SI1440273,SI1321058,SI1320943,SI1320943 SI1440273,SI1321058,SI1320943,SI1320943
Number of entries in each row are not fixed. 每行中的条目数不固定。 First column entries should be my keys. 第一列条目应该是我的密钥。 My code - 我的代码 -
import csv
reader = csv.reader(open('test.csv'))
result = {}
for column in reader:
key = column[0]
if key in result:
pass
result[key] = column[1:]
print result
Output: 输出:
{'SI1440273': ['SI1321058', 'SI1320943', 'SI1320943'], '': ['', '', ''], 'SI1440271': ['SI1320943', '', ''], 'SI1440270': ['SI1320943', 'SI1321085', 'SI1320739'], 'SI1440269': ['SI1320943', 'SI1321085', '']} {'SI1440273':['SI1321058','SI1320943','SI1320943'],'':['','',''],'SI1440271':['SI1320943','',''],' SI1440270':['SI1320943','SI1321085','SI1320739'],'SI1440269':['SI1320943','SI1321085','']}
How can I get rid of null values in the output? 如何在输出中删除空值? Also, how can I have my key values in the output to be in the same order as in csv file? 另外,如何让输出中的键值与csv文件中的键值相同?
Edit: I want single row per 'key' 编辑:我想要每个'键'单行
You could use csv.DictReader
as follows: 您可以使用csv.DictReader
,如下所示:
import csv
result = {}
with open('test.csv') as csvfile:
reader = csv.DictReader(csvfile, delimiter=" ", fieldnames=["id"], restkey="data")
for row in reader:
print row
result[row["id"]] = row["data"]
print result
This would give you a per-row dictionary solution, so you could process it a line at a time. 这将为您提供每行字典解决方案,因此您可以一次处理一行。 I also then append them all into one single result
dictionary. 然后我还将它们全部附加到一个result
字典中。
From this you will get the following output: 从这里你将得到以下输出:
{'data': ['SI1320943', 'SI1321085'], 'id': 'SI1440269'}
{'data': ['SI1320943', 'SI1321085', 'SI1320739', 'SI1440271', 'SI1320943'], 'id': 'SI1440270'}
{'data': ['SI1321058', 'SI1320943', 'SI1320943'], 'id': 'SI1440273'}
{'SI1440273': ['SI1321058', 'SI1320943', 'SI1320943'], 'SI1440270': ['SI1320943', 'SI1321085', 'SI1320739', 'SI1440271', 'SI1320943'], 'SI1440269': ['SI1320943', 'SI1321085']}
try this 尝试这个
import csv
reader = csv.reader(open('test.csv'))
result = {row[0]:row[1:] for row in reader if row and row[0]}
print result
if you want further more to eliminate null in values then do as bellow 如果你想进一步消除值中的null,那么请执行下面的操作
import csv
reader = csv.reader(open('test.csv'))
result = {row[0]:[i for i in row[1:] if i] for row in reader if row and row[0]}
print result
To preserve the order of entry 保持进入顺序
from collections import OrderedDict
result = OrderedDict()
for row in reader:
if row and row[0]:
result[row[0]]=[i for i in row[1:] if i]
# print result
for key in result:
print key,":" ,result[key]
As already noted this is not CSV - so readline and split would be more appropriate and use OrderedDict to keep input order: 如前所述,这不是CSV - 因此readline和split更合适,并使用OrderedDict来保持输入顺序:
import csv
from collections import OrderedDict
result = OrderedDict()
with open('test.csv') as f:
for row in f:
row=row.strip().split()
key = row[0]
result[key] = row[1:]
print result
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.