[Tutor] Handling 'None' (null) values when processing sqlite cursorresults
cwitts at compuscan.co.za
Thu Jul 15 07:37:57 CEST 2010
On 14/07/2010 19:34, Alan Gauld wrote:
> "Christian Witts" <cwitts at compuscan.co.za> wrote
>>> You need a display function that can strip out the nulls as needed.
>>> A simple list comprehension or generator expression would work
>>> in this case:
>> print ' '.join(str(field) for field in data if field is not 'None')
>> The problem with that is if you're relying on a set delimiter you are
>> removing elements so you would be better served by doing `str(field)
>> if field != None else '' for field in record`
> True, although in this case we do seem to have a fixed condition
> since it's the database's representation of null but I originally didn't
> notice the quotes around None. When I fixed that I should have
> changed is to equals..
>> print ' '.join(str(field) for field in data if field != 'None')
> But your modification confuses me. can you explain?
> What would the print line look like?
> Alan G.
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
I prefer not to use a space delimiter, generally a pipe |, so the output
would look a lot like
Av.|Liberal|VT|24742|1-135-197-1139|vehicula.Pellentesque at idmollis.edu|2010-07-13
The reason I suggested that usage instead is because Monte said
>>> having the 'None' values omitted really messed with the list order
which I was depending on
So if you're wanting to create a new list which removes the Nones but
still keeps the same indexing it would be the way to do it, imho.
More information about the Tutor