In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
Today, the editor will share with you the relevant knowledge points about how to solve the json.dumps Chinese garbled problem. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look.
Json.dumps (var,ensure_ascii=False) can not solve the problem of Chinese garbled code.
Json.dumps will behave differently in different versions of Python. Note that the Chinese garbled problem mentioned below does not exist in Python3 versions.
Note: the following code passed the test under python version 2.7
#-*-coding: utf-8-*-odata = {'a': 'Hello'} print odata
Result
{'print json.dumps:'\ xe4\ xbd\ xa0\ xe5\ xa5\ xbd'} 1 print json.dumps (odata)
Result
{"a": "\ u4f60\ u597d"} 1 print json.dumps (odata,ensure_ascii=False)
Result
{"a": "raccoons"} 1 print json.dumps (odata,ensure_ascii=False) .decode ('utf8') .encode (' gb2312')
Result
{"a": "Hello"}
To solve the problem of Chinese encoding, you need to know what python2.7 does with strings:
Because of the function of #-- coding: utf-8--, the contents of the file are encoded in utf-8, so print odata
The output is the result of utf-8 encoding {'asides:'\ xe4\ xbd\ xa0\ xe5\ xa5\ xbd'}
The default ascii encoding for Chinese when json.dumps serialization, and print json.dumps (odata) outputs the result of unicode encoding.
Ascii encoding not used by print json.dumps (odata,ensure_ascii=False), encoded in gbk
"Hello" encoded in utf8 is% E4%BD%A0%E5%A5%BD and decoded in gbk is raccoon.
The representation of a string within Python is unicode encoding.
Therefore, when doing transcoding, it is usually necessary to use unicode as the intermediate encoding, that is, to decode (decode) other encoded strings into unicode, and then from unicode encoding (encode) to another encoding.
The function of decode is to convert other encoded strings into unicode encoding.
Decode ('utf-8') means to convert a utf-8-encoded string into a unicode-encoded string.
The function of encode is to convert unicode encoding into other encoded strings.
Encode ('gb2312'), which translates a unicode-encoded string into a gb2312-encoded string.
There is no such problem in python3, so the easiest way is to introduce the _ _ future__ module to import the features of the new version into the current version
From _ future__ import unicode_literalsprint json.dumps (odata,ensure_ascii=False)
Result
{"a": "Hello"}
Python2.7 's UnicodeEncodeError: 'ascii' codec can't encode exception error occurred while writing to the file
The Great God's solution:
Instead of using open to open the file, use codecs:
From _ _ future__ import unicode_literalsimport codecsfp = codecs.open ('output.txt',' axioms, 'utf-8') fp.write (json.dumps (m-recording ascidence false)) fp.close () these are all the contents of this article entitled "how to solve the problem of garbled codes in json.dumps". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.