How can I determine a Unicode character from its name in Python, even if that character is a control character?

Viewed 1499

I'd like to create an array of the Unicode code points which constitute white space in JavaScript (minus the Unicode-white-space code points, which I address separately). These characters are horizontal tab, vertical tab, form feed, space, non-breaking space, and BOM. I could do this with magic numbers:

whitespace = [0x9, 0xb, 0xc, 0x20, 0xa0, 0xfeff]

That's a little bit obscure; names would be better. The unicodedata.lookup method passed through ord helps some:

>>> ord(unicodedata.lookup("NO-BREAK SPACE"))
160

But this doesn't work for 0x9, 0xb, or 0xc -- I think because they're control characters, and the "names" FORM FEED and such are just alias names. Is there any way to map these "names" to the characters, or their code points, in standard Python? Or am I out of luck?

7 Answers
Related