Escaping certain characters
Robert Kern
rkern at ucsd.edu
Sat Jul 30 19:50:24 EDT 2005
Jan Danielsson wrote:
> Hello,
>
> I'd like to encode the string that outputs:
>
> Hello
> World!
>
> to 'Hello\x0aWorld!', and the string that outputs:
>
> Hello\World!
>
> to 'Hello\\World!'.
>
> Obviously, I want to be able to reverse the process.
>
> I'm going to assume this has already been solved in Python.. But how?
In [1]: s = 'Hello\x0aWorld!'
In [2]: print s
Hello
World!
In [3]: s.encode('string_escape')
Out[3]: 'Hello\\nWorld!'
In [4]: Out[3].decode('string_escape')
Out[4]: 'Hello\nWorld!'
Not *quite* what you asked for, but it ought to be close enough.
--
Robert Kern
rkern at ucsd.edu
"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
More information about the Python-list
mailing list