Python3 processing JSON file containing Chinese dumps

Source: Internet
Author: User

The coding problem of Python3 has been relatively simple

In-memory strings are Unicode

Save to file with Utf-8

The following are the processes that str,byte convert to each other:

str = "ABC Learning "
Str
OUT[6]: ' ABC Learning '
MyByte = str.encode("utf-8")
MyByte
OUT[8]: B ' abc\xe5\xad\xa6\xe4\xb9\xa0 '
STR2 = MyByte. Decode ("utf-8")
str2
OUT[10]: ' ABC Learning '

Recently, when writing JSON-related file access, you encounter such a problem:

Import JSON
Json_str = "" "{"a":" 1","F":"100\n","b":" Study Hard "}" ""
Json_str
OUT[20]: ' {"a": " 1", "F": "100\n", "b": " Study Hard "} '
Json_str = json_str.encode(' Unicode_escape '). Decode (' Utf-8 ')
Json_str
OUT[22]: ' {'a': ' 1', 'f': '100\\n', 'b': '\\u597d\\u597d\\u5b66\\ U4e60"}"
Json_data = json. loads (Json_str, encoding= "utf-8")
Json_data
OUT[24]: {' A ': ' 1 ', ' B ': ' Study hard ', ' f ': ' 100\n '}
DUMPS_STR = json. dumps (Json_data, indent=4)
Dumps_str
OUT[26]: ' {\ n    'f': '100\\n', \ n    'a': ' 1', \ n    'b': '\ \ U597d\\u597d\\u5b66\\u4e60"\ n}"
Json_data = json. loads (DUMPS_STR)
Json_data
OUT[28]: {' A ': ' 1 ', ' B ': ' Study hard ', ' f ': ' 100\n '}

From the above example, through a string into JSON, and then through the json.dumps into a string, the original good learning into the Chinese code, stored in the file is also Chinese encoding, very not intuitive.

After groping, use the following method to make dumps into Chinese (ensure_ascii=false)

DUMPS_STR = json. dumps (Json_data, Ensure_ascii=false, indent=4)
Dumps_str
OUT[30]: ' {\ n    'f': '100\\n', \ n    "a": " 1", \ n    "b": " study hard. ' \ n} '

Python3 processing JSON file containing Chinese dumps

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.