[New-bugs-announce] [issue18679] include a codec to handle escaping only control characters but not any others

Derek Wilson report at bugs.python.org
Wed Aug 7 23:22:45 CEST 2013

New submission from Derek Wilson:

Escaping strings for serialization or display is a common problem. Currently, in python3, in order to escape a sting, you need to do this:


This would give you a string that was represented like this:


But this does not present a suitable representation when the string contains unicode characters. Consider this example:

s = 'Α\tΩ'

There is no method to write this string this with only the control character escaped.

Even python itself recognizes this as a problem and implemented a "solution" for it.

>>> s = 'Α\tΩ'
>>> print(s)
>>> print(repr(s))
>>> print(s.encode('unicode_escape').decode('ascii'))

What I want is public exposure of the functionality to represent control characters with their common \ escape sequences (or \x## for control characters where necessary - for instance unit and record separators).

I have numerous use cases for this and python's own str.__repr__ implementation shows that the functionality is valuable. I would bet that the majority of cases where people use unicode_escape something like a control_escape is more along the lines of what is desired.

And while we're at it, it would be great if this were a unicode->unicode codec like the rot_13 codec. My desired soluiton would look like this:

>>> import codecs
>>> s = 'Α\tΩ'
>>> e = codecs.encode(s, 'control_escape'))
>>> print(e)
>>> print(codecs.decode(e, 'control_escape'))

If this is something that could be included in python 3.4, that would be awesome. I am willing to work on this if so.

components: Library (Lib)
messages: 194625
nosy: underrun
priority: normal
severity: normal
status: open
title: include a codec to handle escaping only control characters but not any others
type: enhancement
versions: Python 3.4

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list