[Python-ideas] CSV api with conversion
MRAB
python at mrabarnett.plus.com
Thu Apr 15 01:02:54 CEST 2010
Bruce Leban wrote:
> If you do this, you'll probably want to support mapping empty values.
> That is,
>
> [int,str]
>
> could map
>
> 1,
> ,2
>
> to
>
> [1,None]
> [None,'2']
>
> or
>
> [1,'']
> [0,'2']
>
> I'm not sure what the defaults should be but there are reasonable use
> cases for both.
>
The actual values read from the CSV file are strings and you're passing
them to functions:
int("1"), str("")
int(""), str("2")
If you want it to return a default value instead of raising an exception
on an empty field then you should pass a conversion function which does
that, for example:
def int_or_none(field):
if field.strip():
return int(field)
else:
return None
> --- Bruce
> http://www.vroospeak.com
>
>
> On Mon, Apr 12, 2010 at 3:24 AM, Éric Araujo <merwok at netwok.org
> <mailto:merwok at netwok.org>> wrote:
>
> Hello list
>
> This API would be very useful. (I’m using Python right know to
> filter hundreds of spreadsheets records. Loving it.)
>
> Suggestions:
> 1) Name the argument “converters” (it’s an iterable);
> 2) Make it a positional argument.
>
> Related wish: Add an argument for a row factory. Default would be
> list, and use cases include using tuple, a named tuple class, or any
> custom callable.
>
> Adding converters and rowfactory would remove the need for looping
> over CSV reader objects and manually using row and cell converters.
>
> Cheers
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org <mailto:Python-ideas at python.org>
> http://mail.python.org/mailman/listinfo/python-ideas
>
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
More information about the Python-ideas
mailing list