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?