[Python-ideas] csv.DictReader could handle headers more intelligently.
Terry Reedy
tjreedy at udel.edu
Fri Jan 25 05:26:19 CET 2013
On 1/24/2013 6:53 PM, Steven D'Aprano wrote:
> DictReader already ignores blank lines, *except for the very first line*.
Interesting. A proper csv file does not contain blank lines. The csv doc
is silent on what is does they are present. (The work 'blank' does not
appear.) Ignoring them seems reasonable, but then all should be ignored.
And the doc should say so.
> Using Python 3.3:
>
> py> from io import StringIO
> py> from csv import DictReader
> py> data = StringIO('spam,ham,eggs\n\n\n\n1,2,3\n\n\n\n\n4,5,6\n')
> py> x = csv.DictReader(data)
> py> next(x)
> {'eggs': '3', 'ham': '2', 'spam': '1'}
> py> next(x)
> {'eggs': '6', 'ham': '5', 'spam': '4'}
>
>
> I don't expect that there is anyone relying on a CSV file with a leading
> blank line to be treated as one having no columns at all:
>
> py> data = StringIO('\n\n\n\nspam,ham,eggs\n1,2,3\n4,5,6\n')
> py> x = DictReader(data)
> py> next(x)
> {None: ['spam', 'ham', 'eggs']}
> py> x.fieldnames
> []
>
>
> I expect that there is probably code that works around this issue, by
> skipping blank lines somehow, e.g.
>
> DictReader(row for row in data if row.strip())
>
> These work-arounds may (or not) be fragile or buggy, but they ought
> to continue working even if DictReader changes its header detection.
--
Terry Jan Reedy
More information about the Python-ideas
mailing list