469,331 Members | 5,302 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,331 developers. It's quick & easy.

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

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
3 13999
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
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
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.

Similar topics

5 posts views Thread by sinister | last post: by
4 posts views Thread by Frank | last post: by
14 posts views Thread by Etu | last post: by
3 posts views Thread by Stef Mientki | last post: by
23 posts views Thread by dkirkdrei | last post: by
7 posts views Thread by JohnF | last post: by
6 posts views Thread by Joseph Stateson | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Purva khokhar | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.