473,434 Members | 4,837 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,434 software developers and data experts.

Splitting lines from a database query

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.
Dec 26 '06 #1
7 1398
ZeD
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
Dec 26 '06 #2
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.
Dec 26 '06 #3
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.

--Scott David Daniels
sc***********@acm.org
Dec 26 '06 #4
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.
Dec 26 '06 #5
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
sc***********@acm.org
Dec 26 '06 #6
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

Dec 26 '06 #7
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

Dec 26 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: robsom | last post by:
Hi, I have a problem with a small python program I'm trying to write and I hope somebody may help me. I'm working on tables of this kind: CGA 1988 06 21 13 48 G500-050 D 509.62 J.. R1 1993 01...
6
by: Earl Anderson | last post by:
I have a A97/XP applet I've developed for my own use in my department. My boss "suggests" that since I built it, I share it with and instruct the other 6 members of my department on its use. I've...
20
by: Ed | last post by:
I am running Access 2002 and just ran the built in Access wizard for splitting a database into a back end (with tables) and front end (with queries, forms, modules, etc.). After running the...
2
by: Gary Lynch | last post by:
I am looking for a simple solution to a recurrent problem with imported data in Access 97. The example below is a simplification of a problem with a much larger database. Let's say I start out...
1
by: Greg Teets | last post by:
I have a field in an access 2000 database that is actually three lines of text separated by carriage returns. Is there a function in the Access version of SQL that will let me locate the...
20
by: Opettaja | last post by:
I am new to c# and I am currently trying to make a program to retrieve Battlefield 2 game stats from the gamespy servers. I have got it so I can retrieve the data but I do not know how to cut up...
5
by: KitKat | last post by:
Right now at work, I get forms e-mailed to me in an Excel chart with a serial number in one column and a new expiration date for a device's trial period in another column. For a while, when...
2
by: shadow_ | last post by:
Hi i m new at C and trying to write a parser and a string class. Basicly program will read data from file and splits it into lines then lines to words. i used strtok function for splitting data to...
4
by: Steven D'Aprano | last post by:
I'm trying to split a URL into components. For example: URL = 'http://steve:secret@www.domain.com.au:82/dir" + \ 'ectory/file.html;params?query#fragment' (joining the strings above with plus...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.