Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Escaping the semicolon?

Reply
Thread Tools

Escaping the semicolon?

 
 
Nick
Guest
Posts: n/a
 
      12-04-2007
Hi all,

Is this expected behavior?

>>> s = '123;abc'
>>> s.replace(';', '\;')

'123\\;abc'

I just wanted a single backslash. I can see why this probably happens
but i wondered if it is definitely intentional.

Thanks
Nick
 
Reply With Quote
 
 
 
 
Bruno Desthuilliers
Guest
Posts: n/a
 
      12-04-2007
Nick a écrit :
> Hi all,
>
> Is this expected behavior?
>
>>>> s = '123;abc'
>>>> s.replace(';', '\;')

> '123\\;abc'


>>> print s.replace(';', '\;')

123\;abc

> I just wanted a single backslash.


You got it - even if it's not obvious !-)

> I can see why this probably happens
> but i wondered if it is definitely intentional.


>>> s2 = '123\;abc'
>>> s2

'123\\;abc'
>>> print s2

123\;abc
>>> list(s2)

['1', '2', '3', '\\', ';', 'a', 'b', 'c']

As you can see, '\\' is counted as a single character !-)
Since the backslash is the escape character, you need to escape it to
have a litteral backslash:

>>> s3 = '\'

File "<stdin>", line 1
s3 = '\'
^
SyntaxError: EOL while scanning single-quoted string
>>>

 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      12-04-2007
Nick wrote:

> Hi all,
>
> Is this expected behavior?
>
>>>> s = '123;abc'
>>>> s.replace(';', '\;')

> '123\\;abc'
>
> I just wanted a single backslash. I can see why this probably happens
> but i wondered if it is definitely intentional.


There is only a single backslash. But the interactive prompt will use the
repr()-function to print out returned values. Which will for strings print
their escaped syntax.

Try the above with a

print s.replace(...)

and you will see your desired outcome.

diez
 
Reply With Quote
 
Tim Chase
Guest
Posts: n/a
 
      12-04-2007
> Is this expected behavior?
>
>>>> s = '123;abc'
>>>> s.replace(';', '\;')

> '123\\;abc'


You're asking the interpreter to print a representation of your
string, so it does so. Representations wrap the results in
quotes and escape characters within that need escaping.

>>> s.replace(';', '\;')

'123\\;abc'
>>> print repr(s.replace(';', '\;'))

'123\\;abc'
>>> print s.replace(';', '\;')

123\;abc

Additionally, it's best-practice to use raw strings or literal
backslashes, making your replacement either

r'\;'

or

'\\;'

because depending on the character following the back-slash, it
may be translated as a character you don't intend it to be.

-tkc


 
Reply With Quote
 
Mel
Guest
Posts: n/a
 
      12-04-2007
Nick wrote:
> Is this expected behavior?
>
>>>> s = '123;abc'
>>>> s.replace(';', '\;')

> '123\\;abc'
>
> I just wanted a single backslash. I can see why this probably happens
> but i wondered if it is definitely intentional.


What you're seeing on the screen is a "literalization" of the string
value for the sake of the display. Consider

Python 2.5.1 (r251:54863, May 2 2007, 16:56:35)
[GCC 4.1.2 (Ubuntu 4.1.2-0ubuntu4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> s = '123;abc'
>>> b = s.replace(';', '\;')
>>> b

'123\\;abc'
>>> len(b)

8


The length suggests that there's only one backslash in the string.


Mel.

On the other hand

>>> repr(b)

"'123\\\\;abc'"

Isn't what I expected. No, wait, it is. It's the value of repr(b)
repred by the Python display logic.

MPW
 
Reply With Quote
 
Nick
Guest
Posts: n/a
 
      12-04-2007
Thanks guys, you answered that interactive prompt question really
clearly however, whats going on here. This works now -

>>> working_string = '123;abc'
>>> search_string = ';'
>>> print working_string.replace(search_string, '\\' + search_string)

123\;abc

But this doesn't -

---
import sys
import string

input = string.join(sys.argv[1:], '')
escape_char_list = [ '(', '[]', ';', '^', '\\', '/', '|', '*', '$',
'&', '[', ']', ')', '?' ]

for ch in escape_char_list:
input = input.replace(ch, '\\' + ch)

print input
---

Try "123 *?/ abc d;o /$'" as the argument... and you get -

123 \*\?\/ abc d\\;o \/\$

Still two back slashes, did i miss something very obvious? Sorry,
sometimes these things are exasperating and just need more eyes or a
head screwed on (if thats the case!).

Nick
 
Reply With Quote
 
Jerry Hill
Guest
Posts: n/a
 
      12-04-2007
On Dec 4, 2007 11:33 AM, Nick <(E-Mail Removed)> wrote:
> Try "123 *?/ abc d;o /$'" as the argument... and you get -
>
> 123 \*\?\/ abc d\\;o \/\$


That's because of the order you're doing the replacement. Put a print
statement inside your for loop and you'll see something like this:

input starts as "123 *?/ abc d;o /$'"
Then when you replace ';' with '\;' you get input = "123 *?/ abc d\;o /$'"
Then the next replacement replaces '\' with '\\' so you get input =
"123 *?/ abc d\\;o /$'"

If you move '\\' to the front of your list of replacement characters,
things will probably work as you expect.

--
Jerry
 
Reply With Quote
 
Nick
Guest
Posts: n/a
 
      12-04-2007

>
> If you move '\\' to the front of your list of replacement characters,
> things will probably work as you expect.
>
> --
> Jerry


I knew it would be something like that! Thanks for your help.
 
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
escaping characters Mike P ASP .Net 1 03-29-2006 03:09 PM
Escaping a CrLf in JavaScript George Durzi ASP .Net 0 07-06-2004 05:03 AM
Re: Escaping a URL in XSL/T Teemu Keiski ASP .Net 0 05-13-2004 04:59 PM
Re: Escaping a URL in XSL/T Swanand Mokashi ASP .Net 0 05-13-2004 04:58 PM
Escaping ' in Javascript ? Ben ASP .Net 4 11-10-2003 06:22 PM



Advertisments