Escaping the semicolon?

Nick nickjbyrne at gmail.com
Tue Dec 4 11:33:01 EST 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



More information about the Python-list mailing list