You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've run into a situation where the CSV input file is very unusual. The Delimiter is "\x06" and the lineterminator is "\x07".
While I've written code to work around this, it would be significantly nicer if the CSV Reader code actually paid attention to the dialect's lineterminator value.
I imagine this is a corner case which will continue to cause problems. At the time the csv module was originally written, I believe the authors' intent was to read and write CSV files which were compatible with Excel. In Python 3, you have to open input files in text mode (that provides the underlying line splitting behavior). Consequently, you're not going to see proper line splitting with unadorned files.
Have you only tried this with Python 3? If you have tried Python 2, were you able to get it to work without your workaround?
So it's untested under python 2, since I've stopped developing under Py2.
I noticed the note in the CSV reader documentation, *AFTER* I diagnosed the issue with the CSV reader... Which is why I opened the bug / feature enhancement request, since this is an very odd edge case.
I agree 90+% of all CSVs are going to be \n line terminated, but if we offer it for writing, we should offer it for reading.
The main emphasis here is this code will not working in the real world, eg.
Looking at your sample file, it seems stranger than you first indicated. Your line terminator actually appears to be '\x07\r\n', not just '\x07'. Opening your file in text mode will leave you with '\x07' as the last character of the last cell in each row. I've attached two files, bell.csv, which has just '\x07' as the line terminator, and lfmapper.py, which provides a class (suboptimally named LFMapper) which takes a file object opened in binary mode and optional line_terminator and encoding args, and performs the necessary slicing of the input bytes, decoding them and returning strings.
Unless Python grows a way for you to tell the open() function what string to use as the line terminator in text mode, I don't think your example is ever going to work without some sort of shim class.
Activity
BenjaminSchollnick commentedon Jul 29, 2019
I've run into a situation where the CSV input file is very unusual. The Delimiter is "\x06" and the lineterminator is "\x07".
While I've written code to work around this, it would be significantly nicer if the CSV Reader code actually paid attention to the dialect's lineterminator value.
tirkarthi commentedon Jul 29, 2019
Seems related : https://bugs.python.org/issue1072404 . There is a note on docs that it's ignored and may be changed in future.
https://docs.python.org/3/library/csv.html
smontanaro commentedon Jul 30, 2019
I imagine this is a corner case which will continue to cause problems. At the time the csv module was originally written, I believe the authors' intent was to read and write CSV files which were compatible with Excel. In Python 3, you have to open input files in text mode (that provides the underlying line splitting behavior). Consequently, you're not going to see proper line splitting with unadorned files.
Have you only tried this with Python 3? If you have tried Python 2, were you able to get it to work without your workaround?
BenjaminSchollnick commentedon Jul 30, 2019
This is tested under python 3...
So it's untested under python 2, since I've stopped developing under Py2.
I noticed the note in the CSV reader documentation, *AFTER* I diagnosed the issue with the CSV reader... Which is why I opened the bug / feature enhancement request, since this is an very odd edge case.
I agree 90+% of all CSVs are going to be \n line terminated, but if we offer it for writing, we should offer it for reading.
The main emphasis here is this code will not working in the real world, eg.
smontanaro commentedon Jul 31, 2019
Looking at your sample file, it seems stranger than you first indicated. Your line terminator actually appears to be '\x07\r\n', not just '\x07'. Opening your file in text mode will leave you with '\x07' as the last character of the last cell in each row. I've attached two files, bell.csv, which has just '\x07' as the line terminator, and lfmapper.py, which provides a class (suboptimally named LFMapper) which takes a file object opened in binary mode and optional line_terminator and encoding args, and performs the necessary slicing of the input bytes, decoding them and returning strings.
Unless Python grows a way for you to tell the open() function what string to use as the line terminator in text mode, I don't think your example is ever going to work without some sort of shim class.