This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author Shane Smith
Recipients Shane Smith
Date 2019-03-03.19:37:04
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1551641825.06.0.900973165633.issue36172@roundup.psfhosted.org>
In-reply-to
Content
It occurred to me there is a slight mismatch in the behavioral consistency of the csv module (at least on Windows, Python 3.X).  Specifically, csv.writer() and csv.reader() treat the line terminator slightly differently.  To boil it down to a concise example:

#==================================================
import csv

data = [[1, 2, 3], [4, 5, 6]]

with open('test.csv', 'w') as fout:
    csv.writer(fout).writerows(data)

with open('test.csv', 'r') as fin:
    data2 = list(csv.reader(fin))
    
print(data, data2, sep='\n')

>>> 
[[1, 2, 3], [4, 5, 6]]
[['1', '2', '3'], [], ['4', '5', '6'], []]
#==================================================

So because csv.writer() uses lineterminator = '\r\n', data and data2 have a different structure (data2 has empty rows).  To me this seems undesirable, so I always go out of my way to use lineterminator = '\n'.  

#==================================================
import csv

data = [[1, 2, 3], [4, 5, 6]]

with open('test.csv', 'w') as fout:
    csv.writer(fout, lineterminator='\n').writerows(data)

with open('test.csv', 'r') as fin:
    data2 = list(csv.reader(fin))
    
print(data, data2, sep='\n')

>>>
[[1, 2, 3], [4, 5, 6]]
[['1', '2', '3'], ['4', '5', '6']]
#==================================================


Then the input and output have the same structure.  I assume there was a reason lineterminator = '\r\n' was chosen as default, but for me there is no benefit wrt csv files.  It seems like we would be better off with the more consistent, "reversible" behavior.

Alternatively, the default behavior of csv.reader() could be changed.  But in either case, I feel like their default behaviors should be in alignment.

Thoughts?  Thanks for reading.
History
Date User Action Args
2019-03-03 19:37:05Shane Smithsetrecipients: + Shane Smith
2019-03-03 19:37:05Shane Smithsetmessageid: <1551641825.06.0.900973165633.issue36172@roundup.psfhosted.org>
2019-03-03 19:37:05Shane Smithlinkissue36172 messages
2019-03-03 19:37:04Shane Smithcreate