Posted By: Anonymous
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 with open('/pythonwork/thefile_subset11.csv', 'w') as outfile: writer = csv.writer(outfile) for row in data: if counter[row] >= 504: writer.writerow(row)
This code reads
thefile.csv, makes changes, and writes results to
However, when I open the resulting csv in Microsoft Excel, there is an extra blank line after each record!
Is there a way to make it not put an extra blank line?
In Python 2, open
outfile with mode
'wb' instead of
rn into the file directly. If you don’t open the file in binary mode, it will write
rrn because on Windows text mode will translate each
In Python 3 the required syntax changed (see documentation links below), so open
outfile with the additional parameter
newline='' (empty string) instead.
# Python 2 with open('/pythonwork/thefile_subset11.csv', 'wb') as outfile: writer = csv.writer(outfile) # Python 3 with open('/pythonwork/thefile_subset11.csv', 'w', newline='') as outfile: writer = csv.writer(outfile)