Translate escaped characters in a string

Discussion in 'Python' started by Ben Finney, Apr 3, 2004.

  1. Ben Finney

    Ben Finney Guest

    On 2 Apr 2004 15:57:59 -0800, - wrote:
    > (\n is slash+n, and not LF)


    No, "\n" is backslash + n. Slash + n would be "/n".

    > I would like to use \n and other supported escape sequences when I
    > write the text to a file.


    Once you know the proper name for the character you're talking about,
    you can find modules like this one:

    <http://www.crazy-compilers.com/py-lib/shellwords.html>

    which may be helpful.

    --
    \ "The best is the enemy of the good." -- Voltaire |
    `\ |
    _o__) |
    Ben Finney <http://bignose.squidly.org/>
    Ben Finney, Apr 3, 2004
    #1
    1. Advertising

  2. Ben Finney

    - Guest

    Hi

    I have an application that reads the arguments sent to the script. If
    an user enters a text like "Line 1\nLine2" (\n is slash+n, and not
    LF), I would like to use \n and other supported escape sequences when
    I write the text to a file.

    Is there a way to do this, other than writing my own parser?
    -, Apr 3, 2004
    #2
    1. Advertising

  3. > I have an application that reads the arguments sent to the script. If
    > an user enters a text like "Line 1\nLine2" (\n is slash+n, and not
    > LF), I would like to use \n and other supported escape sequences when
    > I write the text to a file.
    >
    > Is there a way to do this, other than writing my own parser?


    I know there's an easier way to do it, but the below works...
    >>> import encodings
    >>> print encodings.codecs.escape_decode('hello\\nworld')[0]

    hello
    world
    >>>


    If the stings are escaped normally, the above will produce what you want.

    - Josiah
    Josiah Carlson, Apr 3, 2004
    #3
    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. Replies:
    2
    Views:
    763
  2. Henrik S. Hansen

    Translating escaped characters

    Henrik S. Hansen, Nov 1, 2004, in forum: Python
    Replies:
    2
    Views:
    288
    Henrik S. Hansen
    Nov 2, 2004
  3. Derek Basch
    Replies:
    2
    Views:
    456
    Derek Basch
    Mar 25, 2005
  4. Thea
    Replies:
    2
    Views:
    624
  5. metaperl
    Replies:
    3
    Views:
    332
    Nick Craig-Wood
    Dec 8, 2006
Loading...

Share This Page