Let's say I create a csv with two lines:
>>> import csv
>>> csvfile = csv.writer(open('test.csv', 'w'))
>>> csvfile.writerow(['row'])
5
>>> csvfile.writerow(['row2'])
6
When reading it, I get a final new line:
>>> open('test.csv').read()
'row\nrow2\n'
Of course, this can be expected, but in my case I would prefer not to have it since it would be easier to interpret: No need to check for empty lines when doing a split.
>>> open('test.csv').read().split('\n')
['row', 'row2', '']
解决方案
As a workaround you could use a StringIO() object to write to. The output could then have rstrip() applied to it before writing to a file:
from io