do this with list comp?
Dan Bishop
danb_83 at yahoo.com
Sat Dec 13 03:50:22 EST 2003
"Michael T. Babcock" <mbabcock at fibrespeed.net> wrote in message news:<mailman.107.1071248716.9307.python-list at python.org>...
> python-list-request at python.org wrote:
>
> > From: John Hunter <jdhunter at ace.bsd.uchicago.edu>
> > Date: Fri, 12 Dec 2003 10:46:58 -0600
> >
> >I want to replace all empty fields in a CSV line with 'NULL'.
...
> >I am wondering if there is a way to do it with list comprehensions. I
> >know how I would do it with a ternary operator.....
>
> def replace_blank_with_null(data):
> if not data:
> return "NULL"
> return data
>
> new_list = [ replace_blank_with_null(line) for line in old_list ]
>
> Or something more like:
>
> line_items = [ replace_blank_with_null(item) for item in line.split(",") ]
Or:
line_items = [item or 'NULL' for item in line.split(',')]
More information about the Python-list
mailing list