Translate escaped characters in a string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • -

    Translate escaped characters in a string

    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?
  • Ben Finney

    #2
    Re: Translate escaped characters in a string

    On 2 Apr 2004 15:57:59 -0800, - wrote:[color=blue]
    > (\n is slash+n, and not LF)[/color]

    No, "\n" is backslash + n. Slash + n would be "/n".
    [color=blue]
    > I would like to use \n and other supported escape sequences when I
    > write the text to a file.[/color]

    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/>

    Comment

    • Josiah Carlson

      #3
      Re: Translate escaped characters in a string

      > I have an application that reads the arguments sent to the script. If[color=blue]
      > 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?[/color]

      I know there's an easier way to do it, but the below works...[color=blue][color=green][color=darkred]
      >>> import encodings
      >>> print encodings.codec s.escape_decode ('hello\\nworld ')[0][/color][/color][/color]
      hello
      world[color=blue][color=green][color=darkred]
      >>>[/color][/color][/color]

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

      - Josiah

      Comment

      Working...