[Csv] ignore blank lines?
Skip Montanaro
skip at pobox.com
Fri Feb 14 08:06:10 CET 2003
John> ... I would say make it easier for the caller to detect this
John> situation ...
John> if row == []: return {}
Except the way the DictReader works (and the way I intended for it to work)
is that you specify a default value when creating a reader. When you
encounter a short row, the missing keys are all added to the dictionary,
each associated with the default. An empty dict should never be returned.
You're really trying to treat the CSV file as a table where each row has a
constant number of columns.
This makes sense if you think of this as analogous to using the DB API to
fetch rows from a database table as dictionaries (e.g., c.dictfetchall()
with psycopg or using the cursorclass=DictCursor with MySQLdb. You would
never get an empty dictionary (or sequence, for that matter) corresponding
to an individual row of results. Either it's there with content, or it's
not there at all. It's never there and empty.
I don't have Excel handy at the moment, but I just tried a little experiment
with gnumeric. I entered "abc", "def", and "ghi" in the first three cells
of row 1, jumped down to row 3 and entered "123", "456" and "789" in the
first three cells of that row. I then dumped it as CSV. Here's the result:
abc,def,ghi
,,
123,456,789
Can someone try this with Excel or some other spreadsheet (I'll try
Appleworks in the morning if it occurs to me before I rush out the door)?
Does it produce truly blank lines or does it prevent that by inserting one
or more field separators?
Skip
More information about the Csv
mailing list