Using repr() with escape sequences

Discussion in 'Python' started by nummertolv, Feb 23, 2006.

  1. nummertolv

    nummertolv Guest

    Hi,

    My application is receiving strings, representing windows paths, from
    an external source. When using these paths, by for instance printing
    them using str() (print path), the backslashes are naturally
    interpreted as escape characters.

    >>> print "d:\thedir"

    d: hedir

    The solution is to use repr() instead of str():

    >>> print repr("d:\thedir")

    'd:\thedir'

    What I have not been able to figure out is how to handle escape
    sequences like \a, \b, \f, \v and \{any number} inside the paths. Using
    repr() on these escape sequences either prints the hex value of the
    character (if "unprintable" i guess) or some character ( like in the
    last example below).

    >>> print repr("d:\thedir\10")

    'd:\thedir\x08'

    >>> print repr("d:\thedir\foo")

    'd:\thedir\x0coo'

    >>> print repr("d:\thedir\100")

    'd:\thedir@'

    Could someone clear this out for me and let me know how I can find the
    "real" path that I am trying to receive?

    /Henrik
     
    nummertolv, Feb 23, 2006
    #1
    1. Advertising

  2. On Thu, 23 Feb 2006 07:32:36 -0800, nummertolv wrote:

    > Hi,
    >
    > My application is receiving strings, representing windows paths, from
    > an external source. When using these paths, by for instance printing
    > them using str() (print path), the backslashes are naturally
    > interpreted as escape characters.
    >
    >>>> print "d:\thedir"

    > d: hedir


    No. What is happening here is not what you think is happening.

    > The solution is to use repr() instead of str():


    The solution to what? What is the problem? The way the strings are
    DISPLAYED is surely not the issue, is it?

    >>>> print repr("d:\thedir")

    > 'd:\thedir'



    You have created a string object: "d:\thedir"

    That string object is NOT a Windows path. It contains a tab character,
    just like the print statement shows -- didn't you wonder about the large
    blank space in the string?

    Python uses backslashes for character escapes. \t means a tab character.
    When you enter "d:\thedir" you are embedding a tab between the colon and
    the h.

    The solutions to this problem are:

    (1) Escape the backslash: "d:\\thedir"

    (2) Use raw strings that don't use char escapes: r"d:\thedir"

    (3) Use forward slashes, and let Windows automatically handle them:
    "d:/thedir"

    However, if you are receiving strings from an external source, as you say,
    and reading them from a file, this should not be an issue. If you read a
    file containing "d:\thedir", and print the string you have just read, the
    print statement uses repr() and you will see that the string is just
    what you expect:

    d:\thedir

    You can also check for yourself that the string is correct by looking at
    its length: nine characters.


    --
    Steven.
     
    Steven D'Aprano, Feb 23, 2006
    #2
    1. Advertising

  3. nummertolv

    nummertolv Guest

    I think I might have misused the terms "escape character" and/or
    "escape sequence" or been unclear in some other way because I seem to
    have confused you. In any case you don't seem to be addressing my
    problem.

    I know that the \t in the example path is interpreted as the tab
    character (that was part of the point of the example) and what the
    strings are representing is irrelevant. And yes, the way the strings
    are displayed is part of the issue.

    So let me try to be clearer by boiling the problem down to this:

    - Consider a string variable containing backslashes.
    - One or more of the backslashes are followed by one of the letters
    a,b,f,v or a number.

    myString = "bar\foo\12foobar"

    How do I print this string so that the output is as below?

    bar\foo\12foobar

    typing 'print myString' prints the following:

    bar oo
    foobar

    and typing print repr(myString) prints this:

    'bar\x0coo\nfoobar'


    Hope this makes it clearer. I guess there is a simple solution to this
    but I have not been able to find it. Thanks.

    /H
     
    nummertolv, Feb 23, 2006
    #3
  4. nummertolv enlightened us with:
    > myString = "bar\foo\12foobar"


    Are the interpretations of the escape characters on purpose?

    > How do I print this string so that the output is as below?
    >
    > bar\foo\12foobar


    Why do you want to?

    > typing 'print myString' prints the following:
    >
    > bar oo
    > foobar


    Which is correct.

    Sybren
    --
    The problem with the world is stupidity. Not saying there should be a
    capital punishment for stupidity, but why don't we just take the
    safety labels off of everything and let the problem solve itself?
    Frank Zappa
     
    Sybren Stuvel, Feb 23, 2006
    #4
  5. nummertolv wrote:
    > - Consider a string variable containing backslashes.
    > - One or more of the backslashes are followed by one of the letters
    > a,b,f,v or a number.
    >
    > myString = "bar\foo\12foobar"
    >
    > How do I print this string so that the output is as below?
    >
    > bar\foo\12foobar
    >
    > typing 'print myString' prints the following:
    >
    > bar oo
    > foobar
    >
    > and typing print repr(myString) prints this:
    >
    > 'bar\x0coo\nfoobar'
    >


    The interpretation of escape sequences happens when the Python compiler
    reads the string "bar\foo\12foobar". You'll see that when you do
    something like
    >>> map (ord, "bar\foo\12foobar")

    [98, 97, 114, 12, 111, 111, 10, 102, 111, 111, 98, 97, 114]
    This displays the ASCII values of all the characters.

    If you want to use a string literal containing backslashes, use r'' strings:
    >>> myString = r'bar\foo\12foobar'
    >>> map (ord, myString)

    [98, 97, 114, 92, 102, 111, 111, 92, 49, 50, 102, 111, 111, 98, 97, 114]
    >>> print myString

    bar\foo\12foobar
    >>> print repr (myString)

    'bar\\foo\\12foobar'

    If you get the strings from an external source as suggested by your
    original post, then you really have no problem at all. No interpretation
    of escape sequences takes place when you read a string from a file.

    Daniel
     
    Daniel Dittmar, Feb 23, 2006
    #5
  6. On 23 Feb 2006 08:49:37 -0800, "nummertolv" <>
    declaimed the following in comp.lang.python:

    >
    > myString = "bar\foo\12foobar"
    >

    This is a string literal INSIDE the Python program. The Python
    interpreter sees the escape characters.

    C:\Documents and Settings\Dennis Lee Bieber>python
    ActivePython 2.3.5 Build 236 (ActiveState Corp.) based on
    Python 2.3.5 (#62, Feb 9 2005, 16:17:08) [MSC v.1200 32 bit (Intel)] on
    win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> aString = raw_input()

    bar\foo\12foobar
    >>> print aString

    bar\foo\12foobar
    >>> bString = "bar\foo\12foobar"
    >>> print bString

    bar?oo
    foobar
    >>> print repr(aString)

    'bar\\foo\\12foobar'
    >>> print repr(bString)

    'bar\x0coo\nfoobar'
    >>>



    This one is one that was read from an outside source (I typed just
    bar\foo\12foobar and nothing else). No escape characters are seen
    because the input system does not parse data to create escapes. Each
    character came in as expected.
    --
    > ============================================================== <
    > | Wulfraed Dennis Lee Bieber KD6MOG <
    > | Bestiaria Support Staff <
    > ============================================================== <
    > Home Page: <http://www.dm.net/~wulfraed/> <
    > Overflow Page: <http://wlfraed.home.netcom.com/> <
     
    Dennis Lee Bieber, Feb 23, 2006
    #6
  7. nummertolv

    nummertolv Guest

    myString = "bar\foo\12foobar"
    print repr(myString)

    My "problem" was that I wanted to know if there is a way of printing
    "unraw" strings like myString so that the escape characters are written
    like a backslash and a letter or number. My understanding was that
    repr() did this and it does in most cases (\n and \t for instance). In
    the cases of \a,\b,\f and \v however, it prints hexadecimal numbers.
    But I guess I'll just have to live with that and as you point out, it
    doesn't have to be a problem anyway.
     
    nummertolv, Feb 27, 2006
    #7
    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. Olaf Petzold

    vhdl textio and escape sequences

    Olaf Petzold, Nov 26, 2005, in forum: VHDL
    Replies:
    1
    Views:
    3,536
    Mike Treseler
    Nov 28, 2005
  2. Harald Kirsch
    Replies:
    0
    Views:
    341
    Harald Kirsch
    Nov 19, 2004
  3. Kit

    Escape sequences and printing

    Kit, Sep 16, 2003, in forum: C Programming
    Replies:
    5
    Views:
    521
    Matt Gregory
    Sep 17, 2003
  4. Thomas Philips

    Modifying escape sequences in strings

    Thomas Philips, Mar 2, 2004, in forum: Python
    Replies:
    2
    Views:
    344
    Larry Bates
    Mar 2, 2004
  5. slomo
    Replies:
    5
    Views:
    1,628
    Duncan Booth
    Dec 2, 2007
Loading...

Share This Page