I am trying to make a script that prints a set of characters based on their Unicode names (What I mean by 'Unicode name' is that I want to use the character's 'description', not the codepoint).
I understood that the standard way to do it for a single character is to do:
>>> print('\N{GREEK SMALL LETTER PHI}')
φ
My goal is to use a for loop that goes like this :
unicode_names = ['GREEK SMALL LETTER ALPHA', 'GREEK SMALL LETTER PHI']
for name in unicode_names:
char_name = "{{{0}}}".format(name)
print('\N' + char_name)
and the output would be:
α
φ
The problem is that whatever I try, (so far), I can't print unicode character using a name stored as as string variable, be it by string concatenation or by using a .format()
method.
phi = '\N{GREEK SMALL LETTER PHI}'
phi_name = "{{{0}}}".format('GREEK SMALL LETTER PHI')
print(phi)
print(phi_name)
print('\N' + phi_name)
When I try I get this kind of SyntaxError:
print('\N' + phi_name)
^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-1: malformed \N character escape
Should I use f-strings, or other kind of special strings? Why print('\N{GREEK SMALL LETTER PHI}')
is not interpreted the same as print('\N' + '{GREEK SMALL LETTER PHI}')
?
You're looking for the unicodedata.lookup
function.
>>> import unicodedata
>>> unicodedata.lookup('GREEK SMALL LETTER PHI')
'φ'
Here's a demo of how to use unicodedata
:
import unicodedata
unicode_names = ['GREEK SMALL LETTER ALPHA', 'GREEK SMALL LETTER PHI']
for name in unicode_names:
print (name, unicodedata.lookup(name))
Does this work for you?
unicode_names = ['\N{GREEK SMALL LETTER ALPHA}', '\N{GREEK SMALL LETTER PHI}']
for name in unicode_names:
print(f"{name}")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.