convert ascii escapes into binary form

H

Hans-Peter Jansen

Hi Pythonistas,

I need to convert ascii escapes into binary form, e.g.:
\f -> ^L
\033@\x1bk\000 -> ^[@^[k^@

(rvalues in terminal representation)

Any idea, how to do this most elegantly in python?
Do I really need to do a search n'replace orgy, combined with
regex for this task?

TIA,
Pete
 
R

Robert Kern

Hans-Peter Jansen said:
Hi Pythonistas,

I need to convert ascii escapes into binary form, e.g.:
\f -> ^L
\033@\x1bk\000 -> ^[@^[k^@

(rvalues in terminal representation)

Any idea, how to do this most elegantly in python?
Do I really need to do a search n'replace orgy, combined with
regex for this task?

In [11]: s = '\\f'

In [12]: s.decode('string_escape')
Out[12]: '\x0c'

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

Hans-Peter Jansen

Robert said:
Hans-Peter Jansen said:
Hi Pythonistas,

I need to convert ascii escapes into binary form, e.g.:
\f -> ^L
\033@\x1bk\000 -> ^[@^[k^@

(rvalues in terminal representation)

Any idea, how to do this most elegantly in python?
Do I really need to do a search n'replace orgy, combined with
regex for this task?

In [11]: s = '\\f'

In [12]: s.decode('string_escape')
Out[12]: '\x0c'

That did the trick, thanks a lot, Peter. Unfortunately, on the
target system, there's still python 2.0 running :-( Looks like
I've to bite the apple..

Pete
 
H

Hans-Peter Jansen

Hi Robert,

Hans-Peter Jansen said:
Robert Kern wrote:

That did the trick, thanks a lot, Peter. Unfortunately, on the
s/Peter/Robert/g

Sorry, Robert. That's the price to pay for doing multiple replies at
the same time. Mea culpa..
 

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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top