[Tutor] string on multiple lines

Scott Widney SWidney@ci.las-vegas.nv.us
Wed Nov 13 17:41:15 2002


> Magnus Lycka:
> But comparing it with a string like that seems brittle.
> I mean, if
>   a = '"This", "That"'
> and
>   b = '"This","That"'
> then
>   a == b
> will be false. Is that really what you want?

If it's brittle, then maybe we can reinforce it with some python glue...

> >Chris Kassopulo:
> >but this doesn't work because of the leading spaces:
> >     tst_string='"Series",\
> >         "Denomination",\
> >         "Serial Number",\
> >         "Issue Date"'
> 
> As I wrote before (I think?), you need leading and
> trailing ' on each line.
> 
>      tst_string='"Series",'\
>         '"Denomination",'\
>          '"Serial Number",'\
>         '"Issue Date"'

Before you compare the strings, remove any unnecessary data (in this case
the strings). Here are two function definitions that will achieve that
result. The first one is more backward compatible.

####
def clean(dirtyString):
    '''Gets the white out....'''
    import string
    return string.join(string.split(dirtyString), "")
####
def clean(dirtyString):
    '''New and Improved!'''
    return "".join(dirtyString.split())
####

So let's take a look:

>>> tst_string_CK = '"Series",\
...     "Denomination",\
...     "Serial Number",\
...     "Issue Date"'
>>> tst_stringML = '"Series",'\
...     '"Denomination",'\
...     '"Serial Number",'\
...     '"Issue Date"'
>>> clean(tst_string_CK) == clean(tst_stringML)
1
>>> 

Neat! Note: this solution has been presented on this list before in various
disguises. It might need a little tweaking to work in your situation, but
the basic concept is there.


Scott