By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,203 Members | 1,620 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,203 IT Pros & Developers. It's quick & easy.

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

P: n/a
Hello,

I want to send 3 chars in hexa code to the serial port.

So I type in a tkinter entry : "\x20\x01\x21"

The string is set in a StringVar().

When I read the stringVar, I get : "\\x20\\x01\\x21"

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 ?

I've tried a re.sub(r'\\', chr(92)) but chr(92) is a double backslash again.
Thanks for any help.
Vince.

Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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

Jul 18 '05 #2

P: n/a
Vincent Texier wrote:
Hello,

I want to send 3 chars in hexa code to the serial port.

So I type in a tkinter entry : "\x20\x01\x21"

The string is set in a StringVar().

When I read the stringVar, I get : "\\x20\\x01\\x21"

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 never had a 3 char string: the \ escapes are valid in python code, but not
in strings read from the user by any means. Typing \x20 in a Tkinter entry is
the same as reading a file containing the characters '\', 'x', '2' and '0': you
get exactly these characters, not the character represented by this string if
you had put it in your code.
I've tried a re.sub(r'\\', chr(92)) but chr(92) is a double backslash
again.


No, it's not: you're confusing how it is represented ('\\') and what it is (a
*single* back-slash). Try this:
s = '\\'
s '\\' print s \

Back-slash is an escape character, so you have to double it when you enter it.
But '\\' is a string containing only one back-slash, as you can see when
printing it.

If you're sure you'll never have any quote in the string, you can try to do
eval("'%s'" % myStringVar.get())

Example: s = r'\x20\x01\x21'
s '\\x20\\x01\\x21' eval("'%s'" % s)

' \x01!'

Note that this may be dangerous, since the eval will take place in your program,
so it can break things if you're not careful. Checking that it does not contain
any quotes may be a good start.

HTH
--
- Eric Brunel <eric (underscore) brunel (at) despammed (dot) com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com

Jul 18 '05 #3

P: n/a
Thanks for all, it works !

I will test the presence of quotes to be safe...

Thanks again.
Vince.

Eric Brunel wrote:
If you're sure you'll never have any quote in the string, you can try to
do eval("'%s'" % myStringVar.get())

Example:
>>> s = r'\x20\x01\x21'
>>> s '\\x20\\x01\\x21' >>> eval("'%s'" % s)
' \x01!'

Note that this may be dangerous, since the eval will take place in your
program, so it can break things if you're not careful. Checking that it
does not contain any quotes may be a good start.

HTH

tart.
HTH


Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.