Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of Chinese garbled code in json.dumps in python

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to solve the problem of Chinese garbled code in json.dumps in python". In daily operation, I believe many people have doubts about how to solve the problem of Chinese garbled code in json.dumps in python. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubt of "how to solve the problem of Chinese garbled code in json.dumps in python". Next, please follow the editor to study!

Solving the problem of Chinese garbled codes in python json.dumps

Json Chinese garbled code

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

Results:

{'print json.dumps:'\ xe4\ xbd\ xa0\ xe5\ xa5\ xbd'} print json.dumps (odata)

Results:

{"a": "\ u4f60\ u597d"} print json.dumps (odata,ensure_ascii=False)

Results:

{"a": "raccoons"} print json.dumps (odata,ensure_ascii=False) .decode ('utf8') .encode (' gb2312')

Results:

{"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 () so far, the study on "how to solve the problem of Chinese garbled code in json.dumps in python" is over. I hope I can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report