New submission from Shane <[email protected]>:
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.
----------
messages: 337042
nosy: Shane Smith
priority: normal
severity: normal
status: open
title: csv module internal consistency
type: behavior
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue36172>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com