Python handles Chinese _python in JSON data

Source: Internet
Author: User
Tags true true

Python has its own module for handling python, which can be directly import JSON when used. Using the loads method, you can convert a JSON string into a Python object, as follows:
JSON Python
Object Dict
Array list
String Unicode
Number (int) int, long
Number (real) float
True True
False false
Null None

However, when using the JSON module, it is necessary to pay attention to the processing of Chinese, loads method if the encoding of the incoming string is not UTF-8, you need to use encoding to specify the character encoding

Copy Code code as follows:

Import JSON
Import Base64
f = Open ("./result_diff.txt")
For L in F:
Try
fp = L[l.find ("? FP") +1:]
Http = Fp.find ("http/")
fp = fp[3:http-1]
fp = Fp.decode ("GBK"). Encode ("Utf-8")
STR1 = json.loads (FP, encoding= "Utf-8")
Print str1
Except Exception, E:
Print str (e)

When the FP contains Chinese and is GBK encoded, when we convert the current line encoding to UTF8, we need to specify the encoding when using JSON for decoding.
Or so

Copy Code code as follows:

fp = Fp.decode ("GBK")

Directly into the GBK encoding, you do not have to specify the encoding type of JSON, you do not use the encoding parameter

PS: About JSON operation, here we recommend a few more practical JSON online tools for your reference to use:

Online JSON code inspection, inspection, landscaping, formatting tools:
Http://tools.jb51.net/code/json

JSON Online formatting tool:
Http://tools.jb51.net/code/jsonformat

Online Xml/json Mutual Conversion tool:
Http://tools.jb51.net/code/xmljson

JSON code online Format/beautify/compress/edit/Convert tools:
Http://tools.jb51.net/code/jsoncodeformat

Online JSON compression/escape tool:

Http://tools.jb51.net/code/json_yasuo_trans

C Language Style/html/css/json code formatting landscaping Tools:
Http://tools.jb51.net/code/ccode_html_css_json

Related Article

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.