[New-bugs-announce] [issue36172] csv module internal consistency
Shane
report at bugs.python.org
Sun Mar 3 14:37:05 EST 2019
New submission from Shane <shanesmith4 at gmail.com>:
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 <report at bugs.python.org>
<https://bugs.python.org/issue36172>
_______________________________________
More information about the New-bugs-announce
mailing list