简体   繁体   English

如何从字符串变量中打印 unicode 个字符?

[英]How to print unicode character from a string variable?

I am new in programming world, and I am a bit confused.我是编程界的新手,我有点困惑。

I expecting that both print result the same graphical unicode exclamation mark symbol:我希望这两个打印结果相同的图形 unicode 感叹号符号:

My experiment:我的实验:

number   = 10071
byteStr  = number.to_bytes(4, byteorder='big')
hexStr   = hex(number)
uniChar  = byteStr.decode('utf-32be')
uniStr   = '\\u' + hexStr[2:6]
print(f'{number} - {hexStr[2:6]} - {byteStr} - {uniChar}')

print(f'{uniStr}')   # Not working
print(f'\u2757')     # Working

Output: Output:

10071 - 2757 - b"\x00\x00'W" - ❗
\u2757
❗

What are the difference in the last two lines?最后两行有什么区别? Please, help me to understand it!请帮助我理解它!

My environment is JupyterHub and v3.9 python.我的环境是JupyterHub和v3.9 python。

An escape code evaluated by the Python parser when constructing literal strings. Python 解析器在构造文字字符串时评估的转义码。 For example, the literal string '马' and '马' are evaluated by the parser as the same, length 1, string.例如,文字字符串'马''马'被解析器评估为相同的、长度为 1 的字符串。

You can (and did) build a string with the 6 charactersby using an escape code for the backslash ( \\ ) to prevent the parser from evaluating those 6 characters as an escape code, which is why it prints as the 6-character .您可以(并且确实)通过使用反斜杠 ( \\ ) 的转义码来构建一个包含 6 个字符的字符串,以防止解析器将这 6 个字符评估为转义码,这就是它打印为 6 个字符的原因 .

If you build a byte string with those 6 characters, you can decode it with .decode('unicode-escape') to get the character:如果您使用这 6 个字符构建字节字符串,则可以使用.decode('unicode-escape')对其进行解码以获取字符:

>>> b'\\u2757'.decode('unicode_escape')
'❗'

But it is easier to use the chr() function on the number itself:但是对数字本身使用chr() function 更容易:

>>> chr(0x2757)
'❗'
>>> chr(10071)
'❗'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM