Splitting a string

H

HMS Surprise

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('\\')
 
N

Nick Vatamaniuc

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!).
....:
\'status=no,location=no,width=630,height=550,left=200,top=100\')"
....: target="_blank" class="dvLink" title="Send an Email to
selected
....: employee">''''\'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">''D132258\',\'\',
\'status=no,location=no,width=630,height=550,left=200,top=100\')"
target="_blank" class="dvLink" title="Send an Email to selected
employee">
["'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
 
H

HMS Surprise

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
 
D

Duncan Booth

HMS Surprise said:
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("'")
 
G

Gary Herron

HMS said:
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:
.... \'status=no,location=no,width=630,height=550,left=200,top=100\')"
.... target="_blank" class="dvLink" title="Send an Email to selected
.... employee">''''D132258','',
'status=no,location=no,width=630,height=550,left=200,top=100')"
target="_blank" class="dvLink" title="Send an Email to selected
employee">-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
 
H

HMS Surprise

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.


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!).

....:
\'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">

["'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
 
H

HMS Surprise

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top