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] += 1 writer = csv.writer(open("/path/to/my/csv/file", 'w')) for row in data: if counter[row] >= 4: writer.writerow(row)
counter = collections.defaultdict(int)
defaultdict where the values are integers, and the default value is zero. Then this bit
for row in data: counter[row] += 1
scans the input, and counts how many times the value in the first field occurs in the file. Finally, the code outputs only those rows whose first value occurs at least 4 times. This could all be made a bit shorter by using
collections.Counter instead, but I don’t know whether that was in the standard library ten years ago.