[Tutor] Handling 'None' (null) values when processing sqlite cursorresults

Christian Witts cwitts at compuscan.co.za
Wed Jul 14 14:58:25 CEST 2010


On 14/07/2010 14:32, Alan Gauld wrote:
>
> "Monte Milanuk" <memilanuk at gmail.com> wrote
>
>> (104, None, u'Sylvester', None, u'Evans', None, u'527-9210 Proin 
>> Av.', u'Liberal', u'VT', u'24742', u'1-135-197-1139', 
>> u'vehicula.Pellentesque at idmollis.edu', u'2010-07-13 22:52:50', 
>> u'2010-07-13 22:52:50')
>>
>> At first I was having fits as str.join() was giving me a 'NoneType 
>> error'.  I found one way around that, by processing the results so 
>> the 'None' values got omitted from the list by the time they got to 
>> str.join().
>
> Keep the storage and manipulation of data separate from the
> display of that data. That is an important and fundamental principle
> of data processing. Store the data with the nulls. Display the data 
> without.
>
>> values omitted really messed with the list order which I was 
>> depending on i.e. list[5] could be different fields depending on how 
>> many 'None'
>
> And that is why.
>
>> values had been omitted.  And if I didn't omit them, when I printed 
>> out the user name in the format 'first''middle''last' from the above 
>> record,
>
> 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`

-- 
Kind Regards,
Christian Witts




More information about the Tutor mailing list