I am trying to read a csv file in python 27 to create a dictionary. CSV file looks like-
SI1440269,SI1320943,SI1321085 SI1440270,SI1320943,SI1321085,SI1320739 SI1440271,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', '']}
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?
Edit: I want single row per 'key'
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
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]
You could use csv.DictReader
as follows:
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.
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']}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With