The application scenario is as follows: Download data from the API, JSON parsing, storing in a dictionary, saving regularly. The restart program needs to load the saved text.
The problem 1:json are all Unicode strings, which are stored in the text \u***
Workaround: Turn off the ENSURE_ASCII switch
Json.dump (Pub.listdata,fp,ensure_ascii=false)
Issue 2: Number of dictionary keywords, changed from text load to Unicode string
Solve:
Go a little detour, the online solution, are converted, the string back to Utf-8, the method is
def byteify (input):
If isinstance (input, dict):
return {byteify (key): Byteify (value) for Key,value in Input.iteritems ()}
Elif isinstance (Input, list):
return [Byteify (element) for element in input]
Elif isinstance (Input, Unicode):
Return Input.encode (' Utf-8 ')
Else
return input
But it turns out that JSON is all about Unicode, and it turns around.
The final workaround is to use Unicode, but after load, add a handle to convert the key to a numeric value.
Pub.listdata=json.load (FP)
Pub.listdata={int (k): V for K,v in Pub.listData.items ()}
Issue 3: Coding issues
Solve:
By default, viewing with Sys.getdefaultencoding () is Utf-8
Decode (Code): Convert code to Unicode
Encode (code): Convert Unicode to code
If you call encode on a string in a non-Unicode format, the default encoding is converted to Unicode and then encode. System default encoding is ASCII, so error often
Set the default encoding:
Create a new sitecustomize.py in the Python lib\site-packages folder, with the following content:
# Encoding=utf8
Import Sys
Reload (SYS)
Sys.setdefaultencoding (' UTF8 ')
The processing of JSON in PYTHON27 to Chinese