SQL null conversion

Stefan Schwarzer sschwarzer at sschwarzer.net
Sat Jan 25 17:50:45 EST 2003


Hello Jeff

Jeff Sacksteder wrote:
> I am moving/converting data from a production system to a data warehouse
> using python for the data transformation. I iterate through the result list
> building sql strings to do the inserts that then get sent to the destination
> connection. Occasionally I hit a row where a feild is null, returning
> 'None'. When I try to build a string containing 'None' I get an exception.
> What would be the best way to convert that to an empty string in the general
> case?

If I'm sure that the initial value is None or an empty string, I use
value or '' :

 >>> None or ''
''
 >>> '' or ''
''
 >>> 'Test' or ''
'Test'

However, you might have to deal with unicode strings, so something like
value or u''  may or may not be necessary.

Stefan





More information about the Python-list mailing list