Reversing \N{...} notation?

Tim Chase python.list at tim.thechases.com
Tue Oct 25 13:06:12 EDT 2016


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?

 >>> s = 'ma\N{LATIN SMALL LETTER N WITH TILDE}ana'
 >>> s
 'mañana'
 >>> magic(s)
 'ma\\N{LATIN SMALL LETTER N WITH TILDE}ana'

I can manually rip the string apart and put it back together with
something like

 >>> import unicodedata as ud
 >>> ''.join(c if ord(c) < 128 else '\\N{%s}' % ud.name(c) for c in s)
'ma\\N{LATIN SMALL LETTER N WITH TILDE}ana'

but was wondering if there was some sort of .encode() trick or other
corner of the library I hadn't found.

-tkc







More information about the Python-list mailing list