Splitting lines from a database query

Peter Machell peter at machell.net
Tue Dec 26 15:18:11 EST 2006


ZeD wrote:
> Peter Machell wrote:
> 
>> I have an application where I need to take a query from an existing
>> database and send it to a web api.
> 
> [...]
> 
>> There are always 5 values, but some are blank and some are 'None'.
>> I'd like to split the lines so I get something resembling XML, like this:
>> <FNAME>Frank</FNAME>
>> <SNAME>Spencer</SNAME>
>> <PHONE></PHONE>
>> <DR>Dr I Feelgood</DR>
>> <TIME>2006-12-27 16:00:00</TIME>
> 
> quick and dirty solution:
> 
> bar = (
>  ("Jane Mary","SIMPSON","0411231234","Dr I Feelgood","2006-12-27 15:00:00"),
>  ("John","DOE","None","Dr I Feelgood","2006-12-27 15:30:00"),
>  ("Frank","SPENCER","","Dr I Feelgood","2006-12-27 16:00:00")
> )
> 
> spam ="FNAME", "SNAME", "PHONE", "DR","TIME"
> 
> for x in bar:
>     i=0
>     while i<5:
>         if x[i] != 'None':
>             print "<%s>%s</%s>" % (spam[i], x[i], spam[i])
>         else:
>             print "<%s></%s>" % (spam[i], spam[i])
>         i+=1
>     print
> 

Thanks very much ZeD. This will do what I need to.

The next step is to do some regex on the phone number to ensure it's 
local and correct. How can I split these up so each value has a key?

Peter.



More information about the Python-list mailing list