Tim Chase wrote: > On 2016-10-25 20:14, Peter Otten wrote: >> Tim Chase wrote: >> > I like the clarity of using the "\N{...}" notation when creating >> > string literals involving Unicode chars. >> > >> > Is there a built-in way to get such strings back from Python? >> >> >>> 'mañana'.encode("ascii", "namereplace").decode() >> 'ma\\N{LATIN SMALL LETTER N WITH TILDE}ana' > > Wonderful! Thanks for knowing about that corner of Python and > sharing it. > >> (requires Python 3.5) > > Sorry it wasn't back-ported, but glad it's there now. Thanks!
My original plan was to write a custom error handler, and I found namereplace when I tried to understand the required function signature. Completing the initial effort: $ cat codecs_mynamereplace.py # -*- coding: utf-8 -*- import codecs import unicodedata try: codecs.namereplace_errors except AttributeError: print("using mynamereplace") def mynamereplace(exc): return u"".join( "\\N{%s}" % unicodedata.name(c) for c in exc.object[exc.start:exc.end] ), exc.end codecs.register_error("namereplace", mynamereplace) print(u"mañana".encode("ascii", "namereplace").decode()) $ python3.5 codecs_mynamereplace.py ma\N{LATIN SMALL LETTER N WITH TILDE}ana $ python3.4 codecs_mynamereplace.py using mynamereplace ma\N{LATIN SMALL LETTER N WITH TILDE}ana $ python2.7 codecs_mynamereplace.py using mynamereplace ma\N{LATIN SMALL LETTER N WITH TILDE}ana -- https://mail.python.org/mailman/listinfo/python-list