以Unicode字符获取"实际"字符串长度

use*_*604 11 python unicode-string string-length python-2.7

给定像" ?"(\xe2\x9c\xae)之类的字符,例如,可以是其他像" ?"," ?"或" ?")我想找到字符在屏幕上打印时所占用的"实际"长度

例如

len("?")
len("\xe2\x9c\xae")
Run Code Online (Sandbox Code Playgroud)

两者都返回3,但应该是1

Rah*_*thi 3

你可以这样尝试:

\n\n
unicodedata.normalize(\'NFC\', u\'\xe2\x9c\xae\')\nlen(u"\xe2\x9c\xae")\n
Run Code Online (Sandbox Code Playgroud)\n\n

UTF-8 是一种 unicode 编码,它使用多个字节来表示特殊字符。检查 unicodedata.normalize()

\n

  • 即使这样也不一定计算用户感知的字符或字素簇;变音符号的某些用途没有单代码点表示。我也不明白UTF-8(具体来说)是如何进入图片的? (3认同)