Reputation: 347
I have this code and it creates the dictionary perfectly fine it just keeps creating an extra dictionary entry of '':1 at the very end of the dictionary which I don't need and I don't know how to stop it doing this can someone please help?
with open('coors.csv', mode='r') as infile:
reader = csv.reader(infile)
next(reader, None)
mydict = {columns[0]: 1 for columns in reader}
Upvotes: 3
Views: 40
Reputation: 106946
Apparently there is a blank line at the end of your CSV file, which is treated by csv.reader
as a row with one column of an empty value.
You can simply filter out the empty column with an if
clause in your dict comprehension:
mydict = {column: 1 for column, *_ in reader if column}
Upvotes: 2