[英]Python: convert unicode string to corresponding Unicode character
[英]Convert a string unicode number into represented unicode character
我编写了一个返回生成的 unicode nr 字符串的函数:
def __str__(self):
return '0001F0' + chr(ord('A')+self.color.value-1) + \
hex(self.value.value).lstrip('0x').rstrip('L')
结果:
print(Card(Color(1), Value(2))) #'0001F0A2'
但我不知道如何打印出它所代表的unicode char,即。 你会得到的结果:
print('\U0001F0A2')
尝试像这样添加前缀 '\U':
def __str__(self):
return '\U' + '0001F0' + chr(ord('A')+self.suit.value-1) + \
hex(self.rank.value).lstrip('0x').rstrip('L')
引发错误:
return '\U' + '0001F0' + chr(ord('A')+self.suit.value-1) + \
^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-1: truncated \UXXXXXXXX escape
我一直无法在网上找到解决方案,我尝试过的东西也没有奏效。 我在这里先向您的帮助表示感谢。
可能这会有所帮助:
def f(x):
return chr(int(x, 16))
f('1F0A2')
输出:
'🂢' #'PLAYING CARD TWO OF SPADES'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.