Splitting lines from a database query

ZeD vito.detullio at gmail.com
Tue Dec 26 07:25:19 EST 2006


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

-- 
Under construction



More information about the Python-list mailing list