0

Here is a solution to escape only control characters in a unicode string.

control_chars = [unichr(c) for c in range(0x20)] # you may extend this as required

def control_escape2(s):
    return u''.join([c.encode('unicode_escape') if c in control_chars else c for c in s])

Which is the correct way to encode escape characters in Python 2 without killing Unicode?

This sounds to be a feature that may be worthwhile to put a module so that I don't have to always copy the same code around. Is there anything like this in existing python modules?

user1424739
  • 11,937
  • 17
  • 63
  • 152
  • Can you give us a before-and-after example of the transformation you want? – Robᵩ Nov 15 '17 at 22:52
  • One of the answers to your linked question says this: "*There should be a way to do this in the python stdlib, but there is not. I filed a bug report: http://bugs.python.org/issue18679*". – Robᵩ Nov 15 '17 at 22:54

1 Answers1

0

Yes and no: per this answer, the unicodedata module has utilities related to this, but it does not have an escape_control_characters() function natively.

alex
  • 6,818
  • 9
  • 52
  • 103