Skip to main content
 首页 » 编程设计

python之如何在 Python 中连接和输出 unicode 文本变量

2025年05月04日33dflying

我的标题条款可能不正确,这可能是我无法从网站上找到这个简单内容的原因。

我有一个字符串变量列表。我如何实际连接它们并在 Python 中输出一个真正的 unicode 句子?

base = ['280', '281', '282', '283'] 
end = ['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'] 
unicodes = [u''.join(['\u', j, i]) for j in base for i in end] 
 
for u in unicodes: 
    print u 

我只会得到像 '\u280F' 这样的字符串,而不是真正的字符。但如果我这样做:

print u'\u280F' 

出现正确的符号,即:⠏

而且我确信有一种更优雅的方法来获取从 u2800 到 u283F 的一系列符号...

请您参考如下方法:

将字符串转换为整数(使用 int 和 base 16),使用 unichr (chr 如果您使用的是 Python 3.x)将数字转换为 unicode 对象。

>>> int('280' + 'F', 16)  # => 0x280F, 16: hexadecimal 
10255 
>>> unichr(int('280' + 'F', 16))  # to unicode object 
u'\u280f' 
>>> print unichr(int('280' + 'F', 16)) 
⠏ 

base = ['280', '281', '282', '283'] 
end = ['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'] 
unicodes = [unichr(int(j + i, 16)) for j in base for i in end] 
 
for u in unicodes: 
    print u