constructing binary \n

Discussion in 'Python' started by Steven Arnold, Sep 30, 2004.

  1. Is there a more elegant way to construct \[a-z] in a string than
    something like:

    s = '\\n'
    result = eval( "'%s'" ) % s

    Another ugly method would be to build a dict with all the different
    special letters I want as keys, and their corresponding values as
    values. Or I could have a huge if/elif structure. I can't make ord
    work, because while ord( '\n' ) gives me a reasonable integer that I
    can interpolate with %c, I don't have '\n', I have '\\n'.

    Is there a simple, graceful way to do this sort of translation?

    steve
    Steven Arnold, Sep 30, 2004
    #1
    1. Advertising

  2. Steven Arnold

    Jeff Shannon Guest

    Steven Arnold wrote:

    > Is there a more elegant way to construct \[a-z] in a string than
    > something like:
    >
    > s = '\\n'
    > result = eval( "'%s'" ) % s
    >
    > Another ugly method would be to build a dict with all the different
    > special letters I want as keys, and their corresponding values as
    > values. Or I could have a huge if/elif structure. I can't make ord
    > work, because while ord( '\n' ) gives me a reasonable integer that I
    > can interpolate with %c, I don't have '\n', I have '\\n'.



    No, you actually *do* have '\n', the single byte that represents ASCII
    linefeed.

    >>> len('\n')

    1
    >>> len('\\n')

    2
    >>> ord('\n')

    10
    >>> ord('\\n')

    Traceback (most recent call last):
    File "<interactive input>", line 1, in ?
    TypeError: ord() expected a character, but string of length 2 found
    >>> for char in '\\n':

    .... print ord(char)
    ....
    92
    110
    >>>


    Note that '\n' is a single byte, while '\\n' is two bytes. In the first
    case, '\n' is interpreted as the single LF byte. In the second case,
    '\\' collapses into a single backslash, giving you a backslash byte and
    a 'n' byte.

    When you type a string literal containing a backslash, if that backslash
    can combine with the following character to make a valid escape code, it
    *will* do so unless you've explicitly turned off escaping (by, e.g.,
    using raw strings). Of course, if the combination is *not* a valid
    escape code, then the backslash and following character will be
    interpreted normally.

    Jeff Shannon
    Technician/Programmer
    Credit International
    Jeff Shannon, Sep 30, 2004
    #2
    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. Leo Muller
    Replies:
    2
    Views:
    427
    Paul Glavich [MVP - ASP.NET]
    Jun 16, 2004
  2. Mohammad S Khan

    JDBC - Constructing a query realtime

    Mohammad S Khan, Oct 31, 2003, in forum: Java
    Replies:
    0
    Views:
    395
    Mohammad S Khan
    Oct 31, 2003
  3. Steven Arnold

    Re: constructing binary backslash-n

    Steven Arnold, Sep 30, 2004, in forum: Python
    Replies:
    1
    Views:
    419
    Grant Edwards
    Sep 30, 2004
  4. Tim Peters

    Re: constructing binary backslash-n

    Tim Peters, Sep 30, 2004, in forum: Python
    Replies:
    2
    Views:
    383
    Steve Holden
    Sep 30, 2004
  5. Santosh
    Replies:
    2
    Views:
    2,813
    Santosh
    Jan 16, 2009
Loading...

Share This Page