csv: No fields, or one field?

Neil Cerutti neilc at norwich.edu
Wed Apr 25 14:05:58 EDT 2012


Is there an explanation or previous dicussion somewhere for the
following behavior? I haven't yet trolled the csv mailing list
archive, though that would probably be a good place to check.

Python 3.2 (r32:88445, Feb 20 2011, 21:29:02) [MSC v.1500 32 bit
(Intel)] on win 32
Type "help", "copyright", "credits" or "license" for more information.
>>> import csv
>>> next(csv.reader(['""\r\n']))
['']
>>> next(csv.reader(['\r\n']))
[]

I hoped or assumed that the 2nd invocation should have the same
result as the first. This is not defined anywhere except in a
comment in _csv.c, as far as I can tell. There is no test in
test_csv.py verifying the behavior, either, that I found.

I admit a blank, one-field csv record just isn't very
insteresting, but isn't this a special case that ought to be
documented? The reader requires a blank, one-field record to be
defined with a quoted field, but it doesn't say so in the
documentation.

csv.writer is commutative with the reader in this case though, as
it emits '""\r\n' if you ask it to writerow(['']). This is
surprising given the definition of QUOTE_MINIMAL, which fails to
mention this special case.

  csv.QUOTE_MINIMAL

  Instructs writer objects to only quote those fields which
  contain special characters such as delimiter, quotechar or any
  of the characters in lineterminator.

-- 
Neil Cerutti



More information about the Python-list mailing list