convert ascii escapes into binary form

Discussion in 'Python' started by Hans-Peter Jansen, Jul 20, 2005.

  1. 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
    Hans-Peter Jansen, Jul 20, 2005
    #1
    1. Advertising

  2. Hans-Peter Jansen

    Robert Kern Guest

    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


    "In the fields of hell where the grass grows high
    Are the graves of dreams allowed to die."
    -- Richard Harter
    Robert Kern, Jul 20, 2005
    #2
    1. Advertising

  3. 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
    Hans-Peter Jansen, Jul 20, 2005
    #3
  4. Hi Robert,

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

    > target system, there's still python 2.0 running :-( Looks like
    > I've to bite the apple..
    >
    > Pete
    Hans-Peter Jansen, Jul 20, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marc Schellens
    Replies:
    8
    Views:
    2,978
    John Harrison
    Jul 15, 2003
  2. Replies:
    1
    Views:
    3,649
    Roedy Green
    Jun 9, 2008
  3. Alextophi
    Replies:
    8
    Views:
    481
    Alan J. Flavell
    Dec 30, 2005
  4. bruce
    Replies:
    38
    Views:
    255
    Mark Lawrence
    Nov 1, 2013
  5. MRAB
    Replies:
    0
    Views:
    85
Loading...

Share This Page