CSV Parser and unusual (?) linesterminator - misunderstanding?
Peter Otten
__peter__ at web.de
Fri Jan 20 04:55:40 EST 2006
Tino Lange wrote:
> I'm trying to use the csv Parser included with Python. Field Delimiter is
> "|", Line Delimiter is "#". Unfortunately it doesn't work as expected. The
> parser seems to just ignore the 'lineterminator'?
The csv reader accepts '\r' '\r\n' or '\n' as line endings, even mixed in
the same file. This behaviour is hardcoded. Only the writer uses the
lineterminator specified in the dialect.
The following workaround might suffice
memfile = (s + "\n" for s in "1a|1b|1c|1d#2a|2b|2c|2d#3a|3b|3c
3d#".split("#") if s)
Note that the reader expects a row iterator, so the split operation would be
necessary even if arbitrary line terminators were recognized.
Peter
More information about the Python-list
mailing list