how to replace double backslash with one backslash in string...

Peter Otten __peter__ at web.de
Thu Jul 1 13:03:34 CEST 2004


Vincent Texier wrote:

> I want to send 3 chars in hexa code to the serial port.
> 
> So I type in a tkinter entry : "\x20\x01\x21"

This *is* a 12-char string.
 
> The string is set in a StringVar().
> 
> When I read the stringVar, I get : "\\x20\\x01\\x21"

This is one way to represent above 12-char string in Python.
 
> The length is 12 and not 3 anymore.
> 
> How can I replace the escape character from the string with the
> corresponding char, and get my 3 char string back ?

You are not getting it back, your getting it for the first time:

>>> "\\x20\\x01\\x21".decode("string_escape")
' \x01!'

Peter




More information about the Python-list mailing list