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'
{'SI1440270 SI1320943 SI1321085 SI1320739 SI1440271 SI1320943': [], 'SI1440273 SI1321058 SI1320943 SI1320943': [], 'SI1440269 SI1320943 SI1321085': []}. Can you explain a little more what you want here?print(column). This will print a row of your file, not a column.