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. Advertisements

  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. Advertisements

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. Fangs
    Replies:
    3
    Views:
    10,416
    darshana
    Oct 26, 2008
  2. Marc Schellens
    Replies:
    8
    Views:
    3,342
    John Harrison
    Jul 15, 2003
  3. Steven Arnold

    Re: constructing binary backslash-n

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

    Re: constructing binary backslash-n

    Tim Peters, Sep 30, 2004, in forum: Python
    Replies:
    2
    Views:
    447
    Steve Holden
    Sep 30, 2004
  5. Replies:
    12
    Views:
    803
    Richard Heathfield
    Apr 8, 2007
  6. Ron Eggler

    writing binary file (ios::binary)

    Ron Eggler, Apr 25, 2008, in forum: C++
    Replies:
    9
    Views:
    1,226
    James Kanze
    Apr 28, 2008
  7. Santosh
    Replies:
    2
    Views:
    3,030
    Santosh
    Jan 16, 2009
  8. Don
    Replies:
    60
    Views:
    1,799
    Phred Phungus
    Mar 19, 2010
Loading...