Chinese display Unicode encoding problem when data is written python json file

I. Description of the problem

import json

the dir = {
     ' Spring ' : ' asfffa ' ,
     ' 春眠不觉晓' : ' Everywhere Birds Are Singing ' ,
     ' night comes wind and rain ' : 56789 ,
     ' asdga ' : ' asdasda '
}

fp = open('G:/aa.json', 'w')
fp.write(json.dumps(dir))
fp.close()

On top of this code into the dictionary after json, json file written. When json file open, will find all turned inside Chinese Unicode encoding, as shown in FIG.

{"\u6625\u6653": "asfffa", "\u6625\u7720\u4e0d\u89c9\u6653": "\u5904\u5904\u95fb\u557c\u9e1f", "\u591c\u6765\u98ce\u96e8\u58f0": 56789, "asdga": "asdasda"}

 

 

 

 

 

 Second, the cause

Why export data, Chinese will become Unicode encoding?

json.dumps () method converts dict data to string data, then writes the text string, but json.dumps () method wherein the default mode ascii codes to unicode encoded input to the string.

 

Third, the solution

Added a parameter json.dumps () method, and the value is set to False (the default is True)

fp.write(json.dumps(dir,ensure_ascii=False))

After setting this parameter, the perfect solution

 

Guess you like

Origin www.cnblogs.com/bear7/p/12632167.html