472,374 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,374 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 15517
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: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...

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.