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[10]] += 1
with open('/pythonwork/thefile_subset11.csv', 'w') as outfile:
writer = csv.writer(outfile)
for row in data:
if counter[row[10]] >= 504:
writer.writerow(row)
This code reads thefile.csv, makes changes, and writes results to thefile_subset1.
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?
The
csv.writermodule directly controls line endings and writes\r\ninto the file directly. In Python 3 the file must be opened in untranslated text mode with the parameters'w', newline=''(empty string) or it will write\r\r\non Windows, where the default text mode will translate each\ninto\r\n.If using the
Pathmodule:If using the
StringIOmodule to build an in-memory result, the result string will contain the translated line terminator:If writing that string to a file later, remember to use
newline='':In Python 2, use binary mode to open
outfilewith mode'wb'instead of'w'to prevent Windows newline translation. Python 2 also has problems with Unicode and requires other workarounds to write non-ASCII text. See the Python 2 link below and theUnicodeReaderandUnicodeWriterexamples at the end of the page if you have to deal with writing Unicode strings to CSVs on Python 2, or look into the 3rd party unicodecsv module:Documentation Links