【发布时间】:2017-05-10 02:21:12
【问题描述】:
我想生成所有 utf8 字符列表。
我写了下面的代码,但效果不佳。
我以为是因为chr() 期望 unicode 号码,但我给了 utf8 代码号码。
我想我必须将 utf8 码号转换为 unicode 码号,但我不知道怎么做。
我能怎么做?或者你知道更好的方法吗?
def utf8_2byte():
characters = []
# first byte range: [C2-DF]
for first in range(0xC2, 0xDF + 1):
# second byte range: [80-BF]
for second in range(0x80, 0xBF + 1):
num = (first << 8) + second
line = [hex(num), chr(num)]
characters.append(line)
return characters
我希望:
# UTF8 code number, UTF8 character
[0xc380,À]
[0xc381,Á]
[0xc382,Â]
其实:
[0xc380,쎀]
[0xc381,쎁]
[0xc382,쎂]
【问题讨论】:
标签: python python-3.x unicode utf-8