convert ascii escapes into binary form

Robert Kern rkern at ucsd.edu
Wed Jul 20 12:53:00 EDT 2005


Hans-Peter Jansen wrote:
> 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
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