469,323 Members | 1,560 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,323 developers. It's quick & easy.

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?
Jul 18 '05 #1
2 1437
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/>
Jul 18 '05 #2
> 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
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Vance Kessler | last post: by
13 posts views Thread by Oxns | last post: by
3 posts views Thread by metaperl | last post: by
12 posts views Thread by Torsten Bronger | last post: by
1 post views Thread by bwilcoxis | last post: by
9 posts views Thread by Michael Goerz | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by mdpf | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.