Jason Jason - 3 months ago 15
Python Question

Writing a dict list as rows in CSV

I have a simple csv file:

101,8
102,10
102,6
103,5
104,0


with duplicated entries for row[0] on the second and third line and I want to keep the last (or lower row[1] value) duplicate. The only way I have figured out how to make it work correctly was using a dict() to sort, but now I am having problems writing to a csv file with the correct format. My code:

from operator import itemgetter
from pprint import pprint
import csv

with open('cards1.csv', 'rb') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
with open('cards2.csv', 'wb') as csvfile1:
writer = csv.writer(csvfile1, delimiter=',')
rows = iter(reader)
sort_key = itemgetter(0)
sorted_rows = sorted(rows, key=sort_key)
unique_rows = dict((row[0], row) for row in sorted_rows)
pprint (unique_rows)
writer.writerows(unique_rows)


which prints:

{'101': ['101', '8'],
'102': ['102', '6'],
'103': ['103', '5'],
'104': ['104', '0']}


but writes to my files as:

1,0,2
1,0,3
1,0,1
1,0,4


where as I would like it to simply remove the duplicate in row[0] with the largest value in row[1]. Thanks (btw, the order of the created csv is not critical)

Answer

If I understand correctly.

Instead of:

writer.writerows(unique_rows)

you want to do something like:

for row in unqiue_rows.values():
    writer.writerow(row)