Get alternative char name with unicodedata.name() if no formal one defined

John Machin sjmachin at lexicon.net
Thu Oct 14 20:55:14 EDT 2010


On Oct 14, 7:25 pm, Dirk Wallenstein <hals... at t-online.de> wrote:
> Hi,
> I'd like to get control char names for the first 32 codepoints, but they
> apparently only have an alias and no official name. Is there a way to
> get the alternative character name (alias) in Python?
>

AFAIK there is no programatically-available list of those names. Try
something like:

name = unicodedata.name(x, some_default) if x > u"\x1f" else ("NULL",
etc etc, "UNIT SEPARATOR")[ord(x)]

or similarly with a prepared dict:

C0_CONTROL_NAMES = {
    u"\x00": "NULL",
    # etc
    u"\x1f": "UNIT SEPARATOR",
    }

name = unicodedata.name(x, some_default) if x > u"\x1f" else
C0_CONTROL_NAMES[x]



More information about the Python-list mailing list