Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Splitting a string

Reply
Thread Tools

Splitting a string

 
 
HMS Surprise
Guest
Posts: n/a
 
      05-15-2007
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('\\')

 
Reply With Quote
 
 
 
 
Nick Vatamaniuc
Guest
Posts: n/a
 
      05-15-2007
On May 15, 2:28 pm, HMS Surprise <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
HMS Surprise
Guest
Posts: n/a
 
      05-15-2007
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,heigh t=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

 
Reply With Quote
 
Duncan Booth
Guest
Posts: n/a
 
      05-15-2007
HMS Surprise <(E-Mail Removed)> 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("'")

 
Reply With Quote
 
Gary Herron
Guest
Posts: n/a
 
      05-15-2007
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=2 00,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




 
Reply With Quote
 
HMS Surprise
Guest
Posts: n/a
 
      05-15-2007
On May 15, 2:04 pm, Nick Vatamaniuc <(E-Mail Removed)> wrote:
> On May 15, 2:28 pm, HMS Surprise <(E-Mail Removed)> 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

 
Reply With Quote
 
HMS Surprise
Guest
Posts: n/a
 
      05-15-2007
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Splitting Strings in a map<string, string> byte8bits@gmail.com C++ 3 04-15-2008 06:24 PM
splitting a string into a drop down =?Utf-8?B?SnVzdGlu?= ASP .Net 1 10-25-2004 01:32 AM
Re: Splitting up the definitions of a class into different files (splitting public from private)? John Dibling C++ 0 07-19-2003 04:41 PM
Re: Splitting up the definitions of a class into different files (splitting public from private)? Mark C++ 0 07-19-2003 04:24 PM
Re: Splitting up the definitions of a class into different files (splitting public from private)? John Ericson C++ 0 07-19-2003 04:03 PM



Advertisments