Sebastian Bank <[email protected]> added the comment:
I am not sure about the design vs. code bug distinction, but what makes me
think this should be fixed is primarily the broken round-trip (already
mentioned above):
>>> import io, csv
>>> def roundtrip(value, **fmtparams):
with io.BytesIO() as f:
csv.writer(f, **fmtparams).writerow([value])
f.seek(0)
return next(csv.reader(f, **fmtparams))
>>> roundtrip('spam\neggs', quoting=csv.QUOTE_NONE, escapechar='\\')
['spam\n']
Furthermore, there is the inconsistency between Python 2 and 3, now that this
has been fixed in 3.4.
I agree that the documentation of Dialect.escapechar is not in line with the
code (in both Python 2 and Python 3): How about changing it to something along
the following lines (TODO: reformulate according to how exactly
Dialect.lineterminator affects this)?
"to escape the delimiter, \r, \n, and the quotechar if quoting is set to
QUOTE_NONE
and the quotechar for all other quoting styles if doublequote is False":
>>> def write_csv(value, **fmtparams):
with io.BytesIO() as f:
csv.writer(f, **fmtparams).writerow([value])
return f.getvalue()
>>> write_csv('spam\reggs', quoting=csv.QUOTE_NONE, escapechar='\\')
'spam\\\reggs\r\n'
>>> write_csv('spam\neggs', quoting=csv.QUOTE_NONE, escapechar='\\')
'spam\\\neggs\r\n'
>>> write_csv('spam"eggs', quoting=csv.QUOTE_NONE, escapechar='\\')
'spam\\"eggs\r\n'
>>> write_csv('spam"eggs', quoting=csv.QUOTE_NONE, quotechar=None,
>>> escapechar='\\')
'spam"eggs\r\n'
>>> write_csv('spam"eggs', escapechar='\\', doublequote=False)
'spam\\"eggs\r\n'
> In any case, 'one\nelement' and 'one\\\nelement' are each 2 physical lines.
> I don't see anything in the doc about csv.reader joining physical lines
> into 'logical' lines the way that compile() does.
How about the following?
"csvreader.line_num
The number of lines read from the source iterator. This is not the same as
the number of records returned, as records can span multiple lines."
"On reading, the escapechar removes any special meaning from the following
character."
>>> write_csv('spam\neggs', quoting=csv.QUOTE_NONE) # with delimiter, \r, \n,
>>> and quotechar
Traceback (most recent call last):
...
Error: need to escape, but no escapechar set
>>> roundtrip('spam\neggs')
['spam\neggs']
>>> write_csv('spam\neggs')
'"spam\neggs"\r\n'
----------
nosy: +xflr6
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue15927>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com