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

Discussion in 'Python' started by Vincent Texier, Jul 1, 2004.

  1. 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.
    Vincent Texier, Jul 1, 2004
    #1
    1. Advertising

  2. Vincent Texier

    Peter Otten Guest

    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
    Peter Otten, Jul 1, 2004
    #2
    1. Advertising

  3. Vincent Texier

    Eric Brunel Guest

    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
    Eric Brunel, Jul 1, 2004
    #3
  4. 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
    Vincent Texier, Jul 1, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sydex
    Replies:
    12
    Views:
    6,486
    Victor Bazarov
    Feb 17, 2005
  2. Harry George
    Replies:
    5
    Views:
    734
    Gerrit Holl
    Jan 19, 2004
  3. Fred
    Replies:
    2
    Views:
    367
    John Machin
    Mar 9, 2005
  4. AviraM
    Replies:
    2
    Views:
    6,362
    Manish Pandit
    Sep 28, 2006
  5. Peter Tosh
    Replies:
    7
    Views:
    694
    Peter Tosh
    Jun 12, 2009
Loading...

Share This Page