Create a backslash-escaped version of a string?

Discussion in 'Python' started by boblatest, Feb 8, 2010.

  1. boblatest

    boblatest Guest

    Hello,

    I'd like to have control characters in a string to be converted to
    their backslash-escaped counterparts. I looked in the encoders section
    of the string module but couldn't find anything appropriate. I could
    write it myself but I'm sure something of the sort exists. The
    hypothetical method "c_escaped()" would work like this:

    >>> a="abc\rdef"
    >>> print a.c_escaped()

    abc\rdef
    >>>


    Thanks,
    robert
     
    boblatest, Feb 8, 2010
    #1
    1. Advertising

  2. boblatest

    Chris Rebert Guest

    On Mon, Feb 8, 2010 at 3:14 AM, boblatest <> wrote:
    > Hello,
    >
    > I'd like to have control characters in a string to be converted to
    > their backslash-escaped counterparts. I looked in the encoders section
    > of the string module but couldn't find anything appropriate. I could
    > write it myself but I'm sure something of the sort exists. The
    > hypothetical method "c_escaped()" would work like this:
    >
    >>>> a="abc\rdef"
    >>>> print a.c_escaped()

    > abc\rdef


    print a.encode("string-escape")

    Note that there are some wrinkles if the string contains quote marks
    (and possibly also if it contains Unicode; I didn't test).

    Cheers,
    Chris
    --
    http://blog.rebertia.com
     
    Chris Rebert, Feb 8, 2010
    #2
    1. Advertising

  3. boblatest

    boblatest Guest

    On Feb 8, 12:28 pm, Chris Rebert <> wrote:
    > print a.encode("string-escape")


    How could I miss that? I was on that doc page already. Should have
    typed "/escape" in the browser ;-)

    Thanks,
    robert
     
    boblatest, Feb 8, 2010
    #3
  4. boblatest

    Aahz Guest

    In article <>,
    boblatest <> wrote:
    >
    >I'd like to have control characters in a string to be converted to
    >their backslash-escaped counterparts. I looked in the encoders section
    >of the string module but couldn't find anything appropriate. I could
    >write it myself but I'm sure something of the sort exists. The
    >hypothetical method "c_escaped()" would work like this:
    >
    >>>> a="abc\rdef"
    >>>> print a.c_escaped()

    >abc\rdef
    >>>>


    This is a little different from what you want, but just for the sake of
    completeness:

    >>> print repr(a)[1:-1]

    abc\rdef
    --
    Aahz () <*> http://www.pythoncraft.com/

    "At Resolver we've found it useful to short-circuit any doubt and just
    refer to comments in code as 'lies'. :)"
     
    Aahz, Feb 13, 2010
    #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. Vincent Texier
    Replies:
    3
    Views:
    2,414
    Vincent Texier
    Jul 1, 2004
  2. Petr Jakes
    Replies:
    3
    Views:
    392
    Petr Jakes
    Nov 12, 2005
  3. ldng
    Replies:
    3
    Views:
    1,958
    Tim Golden
    May 10, 2007
  4. V Green
    Replies:
    0
    Views:
    924
    V Green
    Feb 5, 2008
  5. PA Bear [MS MVP]
    Replies:
    0
    Views:
    1,035
    PA Bear [MS MVP]
    Feb 5, 2008
Loading...

Share This Page