This article introduces the Python3 unicode encoding into Chinese problems and solutions, the text of the sample code through the introduction of the very detailed, for everyone's learning or work has a certain reference value of learning, the need for friends can refer to the following
Moved it from another place and collected it for fear that it wouldn't be easily searchable in the future.
The problem I was facing was to send json code out from C++, wrote a server in python and returned it to the C++ program, which I received:
httpSvrDataCbUser: {"tranNO": "0808ad498670dc996", "data": "\u65b0A1EY16", "ver": "1.0", "sendTime": "2019-12-10 00:23:51", "tranType": "2001"}
I sent it over in utf-8 encoding with Chinese in the center, "New A1EY16", but it came back as, "\u65b0A1EY16"
In 9102, most people had already suffered through the unicode inside python2 and moved to python3.
python3 seems to be so friendly to everything unicode, when unicode is present in a string, it can be converted to Chinese to print on the console as long as the string is hardcoded, e.g.:
s = ‘\u7b14\u8bb0' print(s)
What you get is Chinese characters.
But let's say you. The unicode encoding containing '\u' was not hardcoded into the script, but crawled on the web via requests. Then you'll find that what you print out is still unicode as long as this, in other words, the interpreter doesn't even recognize it as unicode at this point, and treats it as a normal sequence of characters.
Baidu found the best solution: add after this unicode string:
s = ().encode(‘unicode_escape') print(s)
It prints out the Chinese characters.
There's no such problem in python3, so the easiest way is to introduce the __future__ module, which imports features from the new version to the current one
from __future__ import unicode_literals print (m,ensure_ascii=False) =>{"a": "Hello."}
Python 2.7 UnicodeEncodeError: 'ascii' codec can't encode exception error when writing to file
The Great God's solution:
Instead of using open to open files, use codecs:
from __future__ import unicode_literals import codecs fp = ('', 'a+', 'utf-8') ((m,ensure_ascii=False)) ()
This is the whole content of this article.