Splitting a string

Discussion in 'Python' started by HMS Surprise, May 15, 2007.

  1. HMS Surprise

    HMS Surprise Guest

    The string s below has single and double qoutes in it. For testing I
    surrounded it with triple single quotes. I want to split off the
    portion before the first \, but my split that works with shorter
    strings does not seem to work with this one.

    Ideas?

    Thanks,
    jvh

    s = ''''D132258\',\'\',
    \'status=no,location=no,width=630,height=550,left=200,top=100\')"
    target="_blank" class="dvLink" title="Send an Email to selected
    employee">'''

    t = s.split('\\')
     
    HMS Surprise, May 15, 2007
    #1
    1. Advertising

  2. On May 15, 2:28 pm, HMS Surprise <> wrote:
    > The string s below has single and double qoutes in it. For testing I
    > surrounded it with triple single quotes. I want to split off the
    > portion before the first \, but my split that works with shorter
    > strings does not seem to work with this one.
    >
    > Ideas?
    >
    > Thanks,
    > jvh
    >
    > s = ''''D132258\',\'\',
    > \'status=no,location=no,width=630,height=550,left=200,top=100\')"
    > target="_blank" class="dvLink" title="Send an Email to selected
    > employee">'''
    >
    > t = s.split('\\')


    jvh,
    For your split operation to work you would need your string to be in
    raw format (add an 'r' in front of it). That way all your back slashes
    won't be interpreted. Or you'll just have to split on ',' instead of
    '\'. The first '\' is not there technically because it just escapes
    the ( ' ). So when your actual string just has a quote ( ' ) an not
    '\'. If it were a raw string, then all your backslashes would have
    been there. (just print s and see what you get!).

    >>> s=r''''D132258\',\'\',

    ....:
    \'status=no,location=no,width=630,height=550,left=200,top=100\')"
    ....: target="_blank" class="dvLink" title="Send an Email to
    selected
    ....: employee">'''
    >>> s

    '\'D132258\\\',\\\'\\\',\n\\
    \'status=no,location=no,width=630,height=550,left=200,top=100\\
    \')"\ntarget="_blank" class="dvLink" title="Send an Email to selected
    \nemployee">'
    >>> print s

    'D132258\',\'\',
    \'status=no,location=no,width=630,height=550,left=200,top=100\')"
    target="_blank" class="dvLink" title="Send an Email to selected
    employee">
    >>> s.split('\\')


    ["'D132258",
    "',",
    "'",
    "',\n",
    "'status=no,location=no,width=630,height=550,left=200,top=100",
    '\')"\ntarget="_blank" class="dvLink" title="Send an Email to selected
    \nemployee">']


    -Nick Vatamaniuc
     
    Nick Vatamaniuc, May 15, 2007
    #2
    1. Advertising

  3. HMS Surprise

    HMS Surprise Guest

    I found my problem, the backslash isn't really there. It is just the
    way it was displayed in the shell after being split from a larger
    string.
    Printing it yields
    D132259','','status=no,location=no,width=630,height=550,left=200,top=100')"
    target="_blank" class="dvLink" title="Send an Email to selected
    employee">

    As as opposed to what I got by just typing the variable name in the
    shell.

    jh
     
    HMS Surprise, May 15, 2007
    #3
  4. HMS Surprise

    Duncan Booth Guest

    HMS Surprise <> wrote:

    > The string s below has single and double qoutes in it. For testing I
    > surrounded it with triple single quotes. I want to split off the
    > portion before the first \, but my split that works with shorter
    > strings does not seem to work with this one.
    >
    > Ideas?
    >
    > Thanks,
    > jvh
    >
    > s = ''''D132258\',\'\',
    > \'status=no,location=no,width=630,height=550,left=200,top=100\')"
    > target="_blank" class="dvLink" title="Send an Email to selected
    > employee">'''
    >
    > t = s.split('\\')
    >
    >


    Remember that the the backslash used as an escape character is purely part
    of the syntax, it doesn't put a backslash into the string. You used triple
    quotes around the string so you didn't need to quote the other single
    quotes as \' in that case the escaping backslash is simply ignored.

    So your string doesn't have any backslash characters in it and splitting on
    a backslash won't do anything useful. If you want to split it before the
    first single quote then use:

    t = s.split("'")
     
    Duncan Booth, May 15, 2007
    #4
  5. HMS Surprise

    Gary Herron Guest

    HMS Surprise wrote:
    > The string s below has single and double qoutes in it. For testing I
    > surrounded it with triple single quotes. I want to split off the
    > portion before the first \, but my split that works with shorter
    > strings does not seem to work with this one.
    >
    > Ideas?
    >
    > Thanks,
    > jvh
    >
    > s = ''''D132258\',\'\',
    > \'status=no,location=no,width=630,height=550,left=200,top=100\')"
    > target="_blank" class="dvLink" title="Send an Email to selected
    > employee">'''
    >
    > t = s.split('\\')
    >

    That can't work because there are no \'s in your string. There are
    backslashes in your program to escape some of the characters from being
    meaningful to the python interpreter. However, once the string is
    parsed and created, it has no backslashes in it. To see this, just
    print it or use find on it:

    >>> s = ''''D132258\',\'\',

    .... \'status=no,location=no,width=630,height=550,left=200,top=100\')"
    .... target="_blank" class="dvLink" title="Send an Email to selected
    .... employee">'''
    >>> print s

    'D132258','',
    'status=no,location=no,width=630,height=550,left=200,top=100')"
    target="_blank" class="dvLink" title="Send an Email to selected
    employee">
    >>> s.find('\\')

    -1

    So the question now becomes: Where do you really want to split it? If
    at the comma then one of these will work for you:

    >>> print s.split(',')[0]

    'D132258'

    >>> i = s.index(',')
    >>> print s[:i]

    'D132258'


    Gary Herron
     
    Gary Herron, May 15, 2007
    #5
  6. HMS Surprise

    HMS Surprise Guest

    On May 15, 2:04 pm, Nick Vatamaniuc <> wrote:
    > On May 15, 2:28 pm, HMS Surprise <> wrote:
    >
    >
    >
    > > The string s below has single and double qoutes in it. For testing I
    > > surrounded it with triple single quotes. I want to split off the
    > > portion before the first \, but my split that works with shorter
    > > strings does not seem to work with this one.

    >
    > > Ideas?

    >
    > > Thanks,
    > > jvh

    >
    > > s = ''''D132258\',\'\',
    > > \'status=no,location=no,width=630,height=550,left=200,top=100\')"
    > > target="_blank" class="dvLink" title="Send an Email to selected
    > > employee">'''

    >
    > > t = s.split('\\')

    >
    > jvh,
    > For your split operation to work you would need your string to be in
    > raw format (add an 'r' in front of it). That way all your back slashes
    > won't be interpreted. Or you'll just have to split on ',' instead of
    > '\'. The first '\' is not there technically because it just escapes
    > the ( ' ). So when your actual string just has a quote ( ' ) an not
    > '\'. If it were a raw string, then all your backslashes would have
    > been there. (just print s and see what you get!).
    >
    > >>> s=r''''D132258\',\'\',

    >
    > ....:
    > \'status=no,location=no,width=630,height=550,left=200,top=100\')"
    > ....: target="_blank" class="dvLink" title="Send an Email to
    > selected
    > ....: employee">'''>>> s
    >
    > '\'D132258\\\',\\\'\\\',\n\\
    > \'status=no,location=no,width=630,height=550,left=200,top=100\\
    > \')"\ntarget="_blank" class="dvLink" title="Send an Email to selected
    > \nemployee">'>>> print s
    >
    > 'D132258\',\'\',
    > \'status=no,location=no,width=630,height=550,left=200,top=100\')"
    > target="_blank" class="dvLink" title="Send an Email to selected
    > employee">
    >
    > >>> s.split('\\')

    >
    > ["'D132258",
    > "',",
    > "'",
    > "',\n",
    > "'status=no,location=no,width=630,height=550,left=200,top=100",
    > '\')"\ntarget="_blank" class="dvLink" title="Send an Email to selected
    > \nemployee">']
    >
    > -Nick Vatamaniuc


    Thanks Nick. However I do not have the option of putting the r in
    front of the source string as it comes the function as a variable from
    another source. Unless it would be permissible to evaluate the
    concantenation some way. But what you have written is instructive and
    I appreciate your time.

    jh
     
    HMS Surprise, May 15, 2007
    #6
  7. HMS Surprise

    HMS Surprise Guest

    Thanks everyone. The shell's display really threw me off. Don't really
    understand why it looks different typing t vs print t. Now that I can
    see past that split works just as advertised. Not real clear on triple
    quotes but I have seen it used and I can see where triple is needed to
    differentiate from the usage of double quotes.


    jvh
     
    HMS Surprise, May 15, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SnVzdGlu?=

    splitting a string into a drop down

    =?Utf-8?B?SnVzdGlu?=, Oct 25, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    291
  2. John Ericson
    Replies:
    0
    Views:
    428
    John Ericson
    Jul 19, 2003
  3. Mark
    Replies:
    0
    Views:
    442
  4. John Dibling
    Replies:
    0
    Views:
    418
    John Dibling
    Jul 19, 2003
  5. Replies:
    3
    Views:
    655
Loading...

Share This Page