Re: Escaping the semicolon?
On Dec 4, 2007 10:40 AM, Nick [EMAIL PROTECTED] wrote: Is this expected behavior? s = '123;abc' s.replace(';', '\;') '123\\;abc' Everything is Ok. It's still a single backslash. Try: print s.replace(';', '\;') Or x = s.replace(';', '\;') print x Best, Sergio -- http://mail.python.org/mailman/listinfo/python-list
Re: Escaping the semicolon?
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Escaping the semicolon?
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 -- http://mail.python.org/mailman/listinfo/python-list
Escaping the semicolon?
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Escaping the semicolon?
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Escaping the semicolon?
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Escaping the semicolon?
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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Escaping the semicolon?
On Dec 4, 2007 11:33 AM, Nick [EMAIL PROTECTED] 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Escaping the semicolon?
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. -- http://mail.python.org/mailman/listinfo/python-list