convert ascii escapes into binary form

Hans-Peter Jansen hpj at urpla.net
Wed Jul 20 20:14:32 CEST 2005


Robert Kern wrote:

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

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



More information about the Python-list mailing list