The processing of JSON in PYTHON27 to Chinese

Source: Internet
Author: User
Tags string back

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.