我使用python 2.7.9将unicode十六进制转换为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
如果我尝试其他文本,如"00DD",它工作良好。知道我的代码有什么问题吗?
如果您需要unicode字符,那么解决方案是使用unichr()而不是chr()。