gpt4 book ai didi

python - 打印 unicode 字符名称 - 例如 'GREEK SMALL LETTER ALPHA' - 而不是 'α'

转载 作者:行者123 更新时间:2023-12-05 08:47:12 25 4
gpt4 key购买 nike

我正在测试函数 isprintable()。我想打印字符串 string.whitespace + unicodedata.lookup("GREEK SMALL LETTER ALPHA") 中所有字符的 Unicode 名称。

如何打印所有名称 - 例如“SPACE”、“NO-BREAK SPACE”、HORIZONTAL TAB、“GREEK SMALL LETTER ALPHA”。

import unicodedata, string

for e in string.whitespace + unicodedata.lookup("GREEK SMALL LETTER ALPHA"):
print(ord(e))
print(unicodedata.name(e))

我收到错误“ValueError:没有这样的名字”

32
SPACE
9
Traceback (most recent call last):
File "<stdin>", line 3, in <module>
ValueError: no such name

最佳答案

如注释所示,Unicode 数据库没有每个字符的名称,但 NameAliases.txt 有。下面解析该文件并返回一个别名(如果存在)。在这种情况下,在文件中找到的第一个:

import string
import requests
import unicodedata as ud

# Pull the official NameAliases.txt from the matching Unicode database
# the current Python was built with.
response = requests.get(f'http://www.unicode.org/Public/{ud.unidata_version}/ucd/NameAliases.txt')

# Parse NameAliases.txt, storing the first instance of a code and a name
aliases = {}
for line in response.text.splitlines():
if not line.strip() or line.startswith('#'):
continue
code,name,_ = line.split(';')
val = chr(int(code,16))
if val not in aliases:
aliases[val] = name

# Return the first alias from NameAliases.txt if it exists when unicodedata.name() fails.
def name(c):
try:
return ud.name(c)
except ValueError:
return aliases.get(c,'<no name>')

for e in string.whitespace + ud.lookup("GREEK SMALL LETTER ALPHA"):
print(f'U+{ord(e):04X} {name(e)}')

输出:

U+0020 SPACE
U+0009 CHARACTER TABULATION
U+000A LINE FEED
U+000D CARRIAGE RETURN
U+000B LINE TABULATION
U+000C FORM FEED
U+03B1 GREEK SMALL LETTER ALPHA

关于python - 打印 unicode 字符名称 - 例如 'GREEK SMALL LETTER ALPHA' - 而不是 'α',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68153407/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com