Simple Problem

N

ncf

I know I've seen this somewhere before, but does anyone know what the
function to escape a string is? (i.e., encoding newline to "\n" and a
chr(254) to "\xfe") (and visa-versa)

Thanks for helping my ignorance :p

-Wes
 
P

Paul Rubin

ncf said:
I know I've seen this somewhere before, but does anyone know what the
function to escape a string is? (i.e., encoding newline to "\n" and a
chr(254) to "\xfe") (and visa-versa)

repr(s)
 
R

Robert Kern

ncf said:
I know I've seen this somewhere before, but does anyone know what the
function to escape a string is? (i.e., encoding newline to "\n" and a
chr(254) to "\xfe") (and visa-versa)

In [1]: s = "foo\n\xfe"

In [2]: s.encode("string_escape")
Out[2]: 'foo\\n\\xfe'

In [3]: repr(s)[1:-1]
Out[3]: 'foo\\n\\xfe'

--
Robert Kern
(e-mail address removed)

"In the fields of hell where the grass grows high
Are the graves of dreams allowed to die."
-- Richard Harter
 
N

ncf

Thank you all for your replies. The repr() solution wasn't exactly what
I was looking for, as I wasn't planning on eval()ing it, but the
(en|de)code solution was exactly what I was looking for. An extended
thanks to Jp for informing me of the version compatibility :)

Have a GREAT day :)
-Wes
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,310
Members
47,977
Latest member
MillaDowdy

Latest Threads

Top