Splitting a string

HMS Surprise john at datavoiceint.com
Tue May 15 15:12:49 EDT 2007


On May 15, 2:04 pm, Nick Vatamaniuc <vatam... at gmail.com> wrote:
> 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

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




More information about the Python-list mailing list