[Csv] Re: dict argument to writer.writerow

Andrew McNamara andrewm at object-craft.com.au
Thu Feb 6 14:01:09 CET 2003


>    Andrew> I don't think this belongs in writer.writerow - I'd suggest it
>    Andrew> belongs in the as yet unwritten csv.util module. The problem is
>    Andrew> that it's going to have an appreciable impact on the normal case
>    Andrew> of writing a tuple.

There's an even better reason now - I've almost completely re-written
the C module so that the reader and writer classes are implemented in C.
I haven't checked the changes in yet, because I need to do some cleaning
up, and I'm too tired - don't make any conflicting changes to _csv.c or
they will be lost.

This should help performance slightly, but the real reason was to sweep
a whole bunch of giblets out of the dialect parsing - my feeling is that
it's a lot cleaner now, but only time will tell.

>Hmmm...  I think of reading/writing dicts as more integration with the DB
>API.  I rarely use plain fetchall() when getting rows from a table.
>Dictionaries are much saner objects.  Accordingly, I'd like it to be as
>painless as possible for people to write them out to CSV files.

Sure. I don't think it's too big an ask that they use an alternate
interface, however.

>Also, one can frequently think of CSV files as a file of dicts with the
>simple optimization that the dictionary keys are only written once, in the
>first row.

Yeah, but then it's something more than a CSV file, isn't it.. 8-)

>That's not to say my code couldn't have been done differently.  I was trying
>hard to avoid testing the type of the object being written.  In retrospect
>the code I have will cause an exception to be raised and caught most of the
>time.  Perhaps it would be better as:
>
>    if hasattr(fields, "has_key"):

The hasattr is about 4 times faster, but by having two interfaces,
we don't even have to pay that cost.

>In any case, I'd like it to be as easy as possible for people to write dicts
>to CSV files and read rows into dicts as possible.

Sure.

-- 
Andrew McNamara, Senior Developer, Object Craft
http://www.object-craft.com.au/


More information about the Csv mailing list