I didn't mean to suggest that errors should be ignored. Making it easy to map an empty field to None instead of throwing an error isn't ignoring an error, it's allowing the developer to preserve data. Granted the csv module doesn't distinguish between empty fields and non-empty fields but many csv files do have that distinction. Perhaps the csv module should be fixed to allow that distinction to be available, maybe by adding new Dialects? Aside from that it's worth considering a variety of use cases before adding something like this to the standard libraries. <div>

<br></div><div>Imagine that I want a missing int in the first field to raise an exception, in the second to get the value 0 and in the third to get the value None, I could write something like:</div><div>(1) csv.reader(f, (int, (int, None), (int, 0))  </div>

<div>(2) csv.reader(f, (int, csv.map_empty(int, None), csv.map_empty(int, 0))</div><div><br></div><div>I'm not saying I like either of those syntaxes, I'm just giving examples to show feasibility. The benefit of either of those is that they can be used for both reading and writing while an arbitrary function can't.</div>

<div><br></div><div>If I was going to add this conversion feature to csv, it seems to me to make the most sense to implement this as a Dialect. The documentation for Dialect is a little thin, but here's an example:</div>

<div><br></div><div>d = Dialect(base_dialect, *ordered_formats, **key_formats) </div><div><br></div><div>d = Dialect(csv.excel, int, str, float)</div><div>d = Dialect(csv.excel, sku=int, name=str, price=float)</div><div>
<br>
</div><div>r = csv.Reader(f, dialect=d)</div><div> <br>--- Bruce<br><a href="http://www.vroospeak.com">http://www.vroospeak.com</a><br>
<br><br><div class="gmail_quote">On Wed, Apr 14, 2010 at 6:45 PM, MRAB <span dir="ltr"><<a href="mailto:python@mrabarnett.plus.com">python@mrabarnett.plus.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">

<div class="im">Bruce Leban wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Sure I could do that. I just this think if you're making a convenience function like this you should strive to make it more convenient.  :-)<br>
<br>
</blockquote></div>
If you're passing in 'int' as a conversion function then it should do<br>
what 'int' does, which is to raise an exception.<br>
<br>
I could quote the Zen of Python:<br>
<br>
    Errors should never pass silently.<br>
    Unless explicitly silenced.<br>
<br>
It shouldn't be so convenient that it leads to sloppy coding. :-)<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
--- Bruce<br>
(via android)<div class="im"><br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
On Apr 14, 2010 4:03 PM, "MRAB" <<a href="mailto:python@mrabarnett.plus.com" target="_blank">python@mrabarnett.plus.com</a> <mailto:<a href="mailto:python@mrabarnett.plus.com" target="_blank">python@mrabarnett.plus.com</a>>> wrote:<br>


<br>
Bruce Leban wrote:<br>
><br>
> If you do this, you'll probably want to support mapping empty values. That is...<br>
<br>
The actual values read from the CSV file are strings and you're passing<br>
them to functions:<br>
<br>
   int("1"), str("")<br>
   int(""), str("2")<br>
<br>
If you want it to return a default value instead of raising an exception<br>
on an empty field then you should pass a conversion function which does<br>
that, for example:<br>
<br>
   def int_or_none(field):<br>
       if field.strip():<br>
          return int(field)<br>
       else:<br>
          return None<br>
<br>
</blockquote></div></blockquote><div class="im">
<br>
_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org" target="_blank">Python-ideas@python.org</a><br>
</div><div><div></div><div class="h5"><a href="http://mail.python.org/mailman/listinfo/python-ideas" target="_blank">http://mail.python.org/mailman/listinfo/python-ideas</a><br>
</div></div></blockquote></div><br></div>