Python character output coding problem

Source: Internet
Author: User

Python Chinese output garbled problem how many beginners, I planted in this area do not know how many somersault. Now I write about the problems and solutions I have come up with to share with you:

1 output garbled

The so-called garbled is refers to "contact language ionizing Juan Po Ying 楃 Juan" such content. Why does it output such a result? Because Python's input and input emphasizes the principle of "inconsistent access". The so-called "discrepancy" refers to what kind of code comes in but does not necessarily output with that encoding. Why is it? Because the Python IDE is only supported for Unicode output, if we are from the file Open data is Utf-8 or ASCII, the output is Unicode output, the result will of course be garbled. So how to solve it?

(1) We do text analysis when the general text file to UTF8 encoding, so first read the text file encoding method set to UTF8, this step must be changed, otherwise how to change the code has no effect;

(2) In the Python header section first join:

#-*-Coding:utf-8-*-

(3) Then add the following three words to the file header:

Import (SYS)

Reload (SYS)

Sys.setdefaultencoding (' Utf-8 ')

This setdefaultencoding is set when the system starts, that is, when you enter Python >>> in the console, the system automatically reads a file, so you must re-reload when you are using Python.

The input will then be successful.

2 Output UTF8 encoding

Output UTF8 encoding This problem I have not solved, the current approach is to import into the TXT file ...

3 Output List garbled

If the list is garbled, the contents of the list are read out and connected to a string:

#print ' [' + ', '. Join (name) + '] '

Python character output coding problem

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.