Came across this topic Save results to csv file with Python
All i needed - to write csv changes to file. BUT. This code stole some of my rows)) unreasonable amount(instead of five as in code)
Could you please explain why do they use collections in this simple action? And why is counter used here?
Use csv.writer:
import csv
with open('thefile.csv', 'rb') as f:
data = list(csv.reader(f))
import collections
counter = collections.defaultdict(int)
for row in data:
counter[row[0]] += 1
writer = csv.writer(open("/path/to/my/csv/file", 'w'))
for row in data:
if counter[row[0]] >= 4:
writer.writerow(row)