简体   繁体   English

如何通过在字符串中使用 \\u 或 \\U 转义来正确表示 python3 (3.6.1+) 中的补充 unicode 字符

[英]How to correctly represent a supplementary unicode char in python3 (3.6.1+) by using \u or \U escape within string

Recently I'm learing python and has encountered a problem with unicode escape literal in python 3.最近我在学习 python 并且在 python 3 中遇到了 unicode 转义文字的问题。

It seems that like Java, the \\u escape is interpreted as UTF-16 code point which Java uses, but here comes the problem:似乎与 Java 一样,\\u 转义符被解释为 Java 使用的 UTF-16 代码点,但问题来了:

For example, if I try to put a 3 bytes utf-8 char like "♬" ( https://unicode-table.com/en/266C/ ) or even supplementary unicode char like "𠜎" ( https://unicode-table.com/en/2070E/ ) by the format of \\uXXXX or \\UXXXXXXXX in a normal string as followed:例如,如果我尝试放置一个 3 个字节的 utf-8 字符,如“♬”( https://unicode-table.com/en/266C/ ),甚至是补充的 unicode 字符,如“𠜎”( https://unicode -table.com/en/2070E/ ) 格式为 \\uXXXX 或 \\UXXXXXXXX 的普通字符串如下:

print('\u00E2\u99AC')  # UTF-8, messy code for sure
print('\U00E299AC')    # UTF-8, with 8 bytes \U, (unicode error) for sure
print('\u266C')        # UTF-16 BE, music note appeares
# from which I suppose \u and \U function the same way they should do in Java
# (may be a little different since they function like macro in Java, and can be useed in comments)

# However, while print('\u266C') gives me '♬','\u266C' == '♬' is equal to false
# which is true in Java semantics.
# Further more, print('\UD841DF0E') didn't give me '𠜎' : (unicode error) 'unicodeescape' codec can't decode bytes in position 0-9: illegal Unicode character
# which I suppose it should be, so it appears to me that I may get it wrong
# Here again : print('\uD841\uDF0E')  # Error, 'utf-8' codec can't encode characters in position 0-1: surrogates not allowed

print('\xD8\x41\xDF\x0E')  # also tried this, messy code
# maybe UTF-16 LE?
print('\u41D8\u0EDF')  # messy code
print('\U41D80EDF')  # error

So, I could see that python "doesn't support supplementary escape literal", and its behavior is also weird.所以,我可以看到python“不支持补充转义文字”,它的行为也很奇怪。

Well, I already know that the correct way to decode and encode such characters:好吧,我已经知道解码和编码这些字符的正确方法:

s_decoded = '\\xe2\\x99\\xac'.encode().decode('unicode-escape')\
               .encode('latin-1').decode('utf-8')
print(b'\xf0\xa0\x9c\x8e'.decode('utf-8'))
print(b'\xd8\x41\xdf\x0e'.decode('utf-16 be'))
assert s_decoded == '♬'

But still don't get how to do it right using \\u \u0026amp; \\U escape literal.但是仍然不知道如何正确使用 \\u \u0026amp; \\U 转义文字。 Hopefully someone could point it out what I'm doing wrong and how it differs from Java's way, thanks!希望有人能指出我做错了什么以及它与 Java 的方式有何不同,谢谢!

By the way, my environment is PyCharm win, python 3.6.1, source code is encoded as UTF-8对了,我的环境是PyCharm win,python 3.6.1,源码编码为UTF-8

Python 3.6.3: Python 3.6.3:

>>> print('\u266c') # U+266C
♬
>>> print('\U0002070E') # U+2070E.  Python is not Java
𠜎
>>> '\u266c' == '♬'
True
>>> '\U0002070E' == '𠜎'
True

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

相关问题 python3 unicode“ \\\\ u7ea2”正确显示 - python3 unicode “\\u7ea2” display correctly 如何在 Python 中解码以“%u”(百分号 + u)开头的 unicode 字符串 - How to decode the unicode string starting with “%u” (percent symbol + u) in Python 3 如何使用\\ u转义码编码Python 3字符串? - How to encode Python 3 string using \u escape code? 如何在JSON字符串中转义unicode语法(u'foo')? - How to escape unicode syntax (u'foo') in JSON string? 使用\\ u转义unicode字符串 - Escaping unicode string using \u 如何在Python中将u'\\\\ u4f60 \\\\ u4f60'这样的unicode字符串转换为u'\\ u4f60 \\ u4f60'? - How to convert unicode string like u'\\u4f60\\u4f60' to u'\u4f60\u4f60' in Python? Python Unicode 字符串在文件中存储为 '\蒸\汽\地',如何将其转换回 Unicode? - Python Unicode string stored as '\u84b8\u6c7d\u5730' in file, how to convert it back to Unicode? Python - unicode字符串中的ASCII编码字符串; 如何删除'你'? - Python - ASCII encoding string in the unicode string; how to remove that 'u'? 如何使Python 2.x Unicode字符串不打印为u'string'? - How to make Python 2.x Unicode strings not print as u'string'? 如何在python中更正一个Unicode字符串,如“ \\ u8bf8 \\ u845b \\ u4eae”? - How to correct one unicode string like “\u8bf8\u845b\u4eae” in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM