473,406 Members | 2,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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 15829
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: sinister | last post by:
The examples in the online manual all seem to use double quotes, e.g. at http://us3.php.net/preg_replace Why? (The behavior is different with single quotes, and presumably simpler to...
4
by: Frank | last post by:
Hi, I'm not familiar with JavaScript but have to fix a problem. Please see below part of the HTML code with contains my question: The problem is that I have to display a path in the showIt...
14
by: Etu | last post by:
Hi, I have a string: string c = "'abc' \"cde\", 'mno' \"xyz\","; how can I use the c.Replace(???, ???) method to have this string: "'abc' "cde", 'mno' "xyz"," that is, all the...
2
by: wylbur37 | last post by:
When using a form with an input textbox such as the following ... <input type="text" name="field1" size=30> I discovered that when a backslash (\) is typed into the textbox, when I later check...
3
by: Stef Mientki | last post by:
It looks like sometimes a single backslash is replaced by a double backslash, but sometimes it's not ??? See the error message below, the first backslash is somewhere (not explicitly in my code)...
2
by: JPS | last post by:
I need to build a string with just one backslash included "\", but I keep getting an error message unless I use "\\". The double backslashes will not work with what I am doing.
23
by: dkirkdrei | last post by:
I am having a bit of trouble trying to double up on slashes in a file path. What I am trying to do is very similar to the code below: <? $var =...
7
by: JohnF | last post by:
I have a function textag($expression){...} whose $expression argument is a string that can contain substrings like \alpha with one backslash or like a&b\\c&d with two backslashes. If I write...
6
by: Joseph Stateson | last post by:
I just started calling a php module from html. I added "php rocket" from microsoft to FP2003 but dont think that is the cause. The problem is that I am getting a backslash before a double or...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.