[Csv] Re: First Cut at CSV PEP

Dave Cole djc at object-craft.com.au
Thu Jan 30 00:25:23 CET 2003


>>>>> "Skip" == Skip Montanaro <skip at pobox.com> writes:

Dave> This is what I was trying to say:
>>>> w = csv.writer(sys.stdio) w.write(['','hello',None])
Skip>     ',hello,\n'
>>>> r = csv.reader(StringIO('None,hello,')) for l in csv: print r
Skip>     ['None','hello','']

Skip> I think we need to limit the data which can be output to
Skip> strings, Unicode strings (if we use an encoded stream), floats
Skip> and ints.  Anything else should raise TypeError.

Dave> Is there any merit having the writer handling non-string data by
Dave> producing an empty field for None, and the result of
Dave> PyObject_Str() for all other values?

Skip> We could do like some of the DB API modules do and provide
Skip> mappings which take the types of objects and see if a function
Skip> exists to handle that type.  If so, whatever that function
Skip> returns would be what was written.  This could handle the case
Skip> of None (allowing the user to specify how it was mapped), but
Skip> could also be used to massage data of known type (for example,
Skip> to round all floats to two decimal places).

Skip> I think this sort of capability should wait until the second
Skip> generation though.

I think this would make things too slow.  The Python core already has
a convenience function for doing the necessary conversion;
PyObject_Str().

If we are in a hurry we could document the existing low level writer
behaviour which is to invoke PyObject_Str() for all non-string values
except None.  None is translated to ''.

Skip> I just tried "never" while saving CSV data from Gnumeric.  It
Skip> didn't escape embedded commas, so it effectively toasted the
Skip> data.

Dave> I have seen that happen in other applications.

Skip> Needless to say, our csv module should *not* do that.  Fried
Skip> data, when accompanied by angry mobs, doesn't taste too good.
Skip> If the user specifies "never", I think an exception should be
Skip> raised if no escape character is defined and fields containing
Skip> the delimiter are encountered.

Should the _csv parser should sanity check the combination of options
in the constructor, or when told to write data which is broken?

It is possible to define no quote or escape character but still write
valid data.

  1,2,3,4

- Dave

-- 
http://www.object-craft.com.au

_______________________________________________
Csv mailing list
Csv at mail.mojam.com
http://manatee.mojam.com/mailman/listinfo/csv



More information about the Csv mailing list