Splitting lines from a database query

John Machin sjmachin at lexicon.net
Tue Dec 26 17:59:04 EST 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




More information about the Python-list mailing list