[Csv] Re: First Cut at CSV PEP
Cliff Wells
LogiplexSoftware at earthlink.net
Thu Jan 30 21:22:14 CET 2003
On Thu, 2003-01-30 at 06:51, Skip Montanaro wrote:
> Dave> The None thing and the implicit __str__ conversion is there in the
> Dave> Object Craft parser to be compatible with the DB-API....
>
> Hmmm... I've used MySQLdb and psycopg and don't recall my queries returning
> None. (He furiously searches for None in PEP 249...) Ah, I see:
>
> SQL NULL values are represented by the Python None singleton on input
> and output.
>
> I generally have always defined my fields to have defaults and usually also
> declare them NOT NULL, so I wouldn't expect to see None in my query results.
>
> Still, the current treatment of None doesn't successfully round-trip
> ("select * ...", dump to csv, load from csv, repopulate database). Do you
> distinguish an empty field from a quoted field printed as ""? That is, are
> these output rows different?
>
> 5.0,,"Mary, Mary, quite contrary"\r\n
> 5.0,"","Mary, Mary, quite contrary"\r\n
>
> the former parsing into
>
> [5.0, None, "Mary, Mary, quite contrary"]
>
> and the latter into
>
> [5.0, "", "Mary, Mary, quite contrary"]
I'd suggest *not* mapping anything to any object but a string on
*import*. CSV files don't have any way of carrying type information
(except perhaps on an application-by-application basis, but I don't
think that's where we're going here) so it's best to treat *everything*
as a string.
Export is a slightly different story. I do think None should be mapped
to '' on export since that is the only reasonable value for it, and
there are enough existing modules that use None to represent an empty
value that this would be a reasonable thing for us to handle.
>
> Dave> Now without the implicit __str__ and conversion of None to '' we
> Dave> would require a shirtload of code to do the same thing, only it
> Dave> would be as slow as a slug on valium.
>
> How about we let the user define how to handle None? I would always want
> None's appearing in my data to raise and exception. You clearly have a use
> case for automatically mapping to the empty string.
This might not affect performance too badly if we *always* raise an
exception when passed anything but a string, and do the conversion
(which would involve a table lookup) in the exception handler. Anything
not in the table would cause the exception to be passed up to the
caller.
That being said, this might complicate things too much for many people.
--
Cliff Wells, Software Engineer
Logiplex Corporation (www.logiplex.net)
(503) 978-6726 x308 (800) 735-0555 x308
More information about the Csv
mailing list