Splitting a string
Nick Vatamaniuc
vatamane at gmail.com
Tue May 15 15:04:22 EDT 2007
On May 15, 2:28 pm, HMS Surprise <j... at datavoiceint.com> 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
More information about the Python-list
mailing list