Python使用'' .join转换unicode(TypeError:序列0:期望字符串,找到int)

时间:2016-10-12 08:40:55

标签: python unicode hex

我使用python 2.7.9将unicode hex转换为unicode文本,但我仍然坚持使用以下代码:

text = '0421'
converted_text = ''.join([chr(int(''.join(c), 16)) for c in zip(text[0::4], text[1::4], text[2::4], text[3::4])])
print converted_text

ValueError: chr() arg not in range(256)

当我删除chr()时:

converted_text = ''.join([int(''.join(c), 16) for c in zip(text[0::4], text[1::4], text[2::4], text[3::4])])

TypeError: sequence item 0: expected string, int found

如果我尝试其他文字,例如&00; 00DD'它工作正常。 知道我的代码中有什么问题吗?

1 个答案:

答案 0 :(得分:1)

如果你需要unicode字符,那么解决方案是使用unichr()而不是chr()。