The output of a list or dictionary containing UTF-8 encoded Chinese in python

Source: Internet
Author: User

Under Python, a list or dictionary that contains a Chinese string, using print directly shows the following results:

Dict = {"ASDF": "Our Python Learning"}
Print Dict
{' asdf ': ' \xe6\x88\x91\xe4\xbb\xac\xe7\x9a\x84python\xe5\xad\xa6\xe4\xb9\xa0 '}
It is inconvenient to output a well processed data structure and needs to be output using the following methods:
Import JSON
Print Json.dumps (dict, encoding= "UTF-8", Ensure_ascii=false)
{"ASDF": "Our Python Learning"}
Note the above two parameters of my experience:

If there are dict, it's okay. I took it from the MONGO, so I needed json.loads, but there was always a problem. So this plan is not for me.

If it is a string, direct output or
Print Str.encode ("UTF-8")

Used for other encodings as well. Try it quickly.

In editing a scheme for someone else. Unauthenticated.

All_symptom Content

[Python] View plaincopy the code to see the snippet derived from my Code slice
[u ' \u773c ', U ' \u8179\u90e8 ', U ' \u4e94\u5b98 ', U ' \u53e3\u8154 ', U ' \u8179\u90e8 ',
U ' \u53e3\u8154 ']

[Python] View plaincopy the code to see the snippet derived from my Code slice
Str_symptom = str (all_symptom). Replace ("u\", "\")
Str_symptom.decode ("Unicode-escape") my experience

This program is my later use of the program, uncertainty efficiency. But it works.
my answer is Str_symptom.decode ("Unicode-escape"). Encode ("Utf-8"). After decoding with Unicode, use the Utf-8 code.

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.