Message337042
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. |
|
Date |
User |
Action |
Args |
2019-03-03 19:37:05 | Shane Smith | set | recipients:
+ Shane Smith |
2019-03-03 19:37:05 | Shane Smith | set | messageid: <1551641825.06.0.900973165633.issue36172@roundup.psfhosted.org> |
2019-03-03 19:37:05 | Shane Smith | link | issue36172 messages |
2019-03-03 19:37:04 | Shane Smith | create | |
|