1 回答

TA貢獻1815條經(jīng)驗 獲得超6個贊
想必 UTF-8 也有類似的方法嗎?
你想知道哪些代碼點在 ascii 范圍之外是可打印的嗎?或者你想要可打印字符的 utf8 編碼?
要獲取所有 unicode 的所有可打印代碼點:
unicode_max = 0x10ffff
printable_glyphs = [ chr(x) for x in range(0, unicode_max+1) if chr(x).isprintable() ]
上面說了,utf8是一種編碼。那時文本被映射到特定的字節(jié),以便其他程序可以共享數(shù)據(jù)。
內(nèi)存中的文本不是 utf8。每個字符/字形都有一個代碼點。
轉換為 utf-8
import unicodedata
monkey = unicodedata.lookup('monkey')
print(f"""
glyph: {monkey}
codepoint: Dec: {ord(monkey)}
codepoint: Hex: {hex(ord(monkey))}
utf8: { monkey.encode('utf8', errors='strict') }
utf16: { monkey.encode('utf16', errors='strict') }
utf32: { monkey.encode('utf32', errors='strict') }
""")
輸出:
glyph: ??
codepoint: Dec: 128018
codepoint: Hex: 0x1f412
utf8: b'\xf0\x9f\x90\x92'
utf16: b'\xff\xfe=\xd8\x12\xdc'
utf32: b'\xff\xfe\x00\x00\x12\xf4\x01\x00'
添加回答
舉報