Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Splitting lines from a database query

Reply
Thread Tools

Splitting lines from a database query

 
 
Peter Machell
Guest
Posts: n/a
 
      12-26-2006
I have an application where I need to take a query from an existing
database and send it to a web api.

Here's a cut down version of my existing code:

for foo in mssql.fetch_array();
bar = foo[2] #trims the first result which we don't use
for x in bar:
for y in x:
print y
print

This produces each value on a line, here's an example result:

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

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>

Thanks in advance for your help,
Peter.


 
Reply With Quote
 
 
 
 
ZeD
Guest
Posts: n/a
 
      12-26-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
 
Reply With Quote
 
 
 
 
Peter Machell
Guest
Posts: n/a
 
      12-26-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.
 
Reply With Quote
 
Peter Machell
Guest
Posts: n/a
 
      12-26-2006
Scott David Daniels wrote:
> Peter Machell wrote:
>> ZeD wrote: <An excellent response providing code>
>>
>> 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?

>
> Well, you should try that, unless you intend to get the newsgroup to
> write your code for you. Come back with your efforts and any problems
> you have with them.


As we say in Australia, fair enough.
I can almost do it this way:

for x in bar:
fname = x[0]
if fname == "":
fname == "None"
sname = x[1]
if sname == "":
sname == "None"

print "<FNAME>"+fname+"</FNAME>"+"<SNAME>"+sname+"</SNAME>"

Except that I should be using a list and loop to do the null checking,
and it still stops when (I think) it hits a blank value:
TypeError: cannot concatenate 'str' and 'NoneType' objects

thanks,
Peter.
 
Reply With Quote
 
Scott David Daniels
Guest
Posts: n/a
 
      12-26-2006
Peter Machell wrote:
> I can almost do it this way:
>
> for x in bar:
> fname = x[0]
> if fname == "":
> fname == "None"
> sname = x[1]
> if sname == "":
> sname == "None"
>
> print "<FNAME>"+fname+"</FNAME>"+"<SNAME>"+sname+"</SNAME>"


for this:
for row in bar:
print "<FNAME>%s</FNAME><SNAME>%s</SNAME>" % (
row[0] or 'None', row[1] or 'None')


> Except that I should be using a list and loop to do the null checking,
> and it still stops when (I think) it hits a blank value:
> TypeError: cannot concatenate 'str' and 'NoneType' objects

What's the data and program that does that?

--Scott David Daniels
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Gabriel Genellina
Guest
Posts: n/a
 
      12-26-2006
At Tuesday 26/12/2006 18:57, Peter Machell wrote:

>for x in bar:
> fname = x[0]
> if fname == "":
> fname == "None"
> sname = x[1]
> if sname == "":
> sname == "None"
>
> print "<FNAME>"+fname+"</FNAME>"+"<SNAME>"+sname+"</SNAME>"
>
>Except that I should be using a list and loop to do the null checking,
>and it still stops when (I think) it hits a blank value:
> TypeError: cannot concatenate 'str' and 'NoneType' objects


Perhaps this is what you intended to write:

if fname == "":
fname = "None"

and since None != "", you have to test for it:

if fname is None or fname == "":
fname = "None"

Note that doing this will deliberately write the text "None" whenever
the field is empty, so you will never get <FNAME></FNAME>, instead:
<FNAME>None</FNAME>. If you want to get the former, use: if fname is
None: fname = ""


--
Gabriel Genellina
Softlab SRL






__________________________________________________
Preguntá. Respondé. Descubrí.
Todo lo que querías saber, y lo que ni imaginabas,
está en Yahoo! Respuestas (Beta).
¡Probalo ya!
http://www.yahoo.com.ar/respuestas

 
Reply With Quote
 
John Machin
Guest
Posts: n/a
 
      12-26-2006
Peter Machell wrote:
> Scott David Daniels wrote:
> > Peter Machell wrote:
> >> ZeD wrote: <An excellent response providing code>
> >>
> >> 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?

> >
> > Well, you should try that, unless you intend to get the newsgroup to
> > write your code for you. Come back with your efforts and any problems
> > you have with them.

>
> As we say in Australia, fair enough.
> I can almost do it this way:


As we say in Australia, it's good to see that you're neither a bludger
nor an utter dill/drongo/nong

>
> for x in bar:


Insert some code to show you what you've actually got:

print repr(x)

> fname = x[0]
> if fname == "":
> fname == "None"
> sname = x[1]
> if sname == "":
> sname == "None"
>
> print "<FNAME>"+fname+"</FNAME>"+"<SNAME>"+sname+"</SNAME>"
>
> Except that I should be using a list and loop to do the null checking,


That's *not* a null that you're checking for, mate, it's a zero-length
string.

> and it still stops when (I think) it hits a blank value:
> TypeError: cannot concatenate 'str' and 'NoneType' objects
>


It's not a "blank value", it's an object None which you get as a
substitute for a NULL in the database; it means "no value at all", as
opposed to a zero-length string, which is a value.

You can test for None by:
if thing is None:
You can test for both "" and None in one hit by doing this:
if not thing:

BTW, why do you want to fill in the missing data with the string value
"None" (which could conceivably be a valid surname), instead of leaving
it blank or empty?

Cheers,
John

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Database Database Database Database scott93727@gmail.com Computer Information 0 09-27-2012 02:43 AM
DataBase DataBase DataBase DataBase scott93727@gmail.com Computer Information 0 09-26-2012 09:40 AM
Re: Splitting up the definitions of a class into different files (splitting public from private)? John Dibling C++ 0 07-19-2003 04:41 PM
Re: Splitting up the definitions of a class into different files (splitting public from private)? Mark C++ 0 07-19-2003 04:24 PM
Re: Splitting up the definitions of a class into different files (splitting public from private)? John Ericson C++ 0 07-19-2003 04:03 PM



Advertisments