Python output Chinese--How to print in Windows console is not garbled

Source: Internet
Author: User

Article Introduction

Using Python to print Chinese in the console is not garbled, it's been a lot of pythoner problems, and even a lot of Python veteran often puzzled. The reason is that from the general Web page, database or text from the external data source crawling over the content, need to pass the correct codec to normal output, and Python coding and decoding mechanism is more complex, without deep thinking of the words often encountered debugging errors. This article describes the most common cases of output Chinese, the compilation platform for the Windows console, the Python version of 2.7.9

Some of the contents of the article refer to the Click Open link to thank the original author for sharing.


Pre-knowledge

STR and Unicode

First you need to figure out the difference between STR and Unicode. Str is a string of bytes, made up of Unicode encoded (encode) bytes . Unicode is the real string, made up of characters . During the programming process, it is important to understand whether to handle STR or Unicode, using the pair processing method (Str.decode/unicode.encode). A simple principle is: Do not use encode to STR, do not use decode for Unicode (in fact Str can use encode, but it is not recommended to do so, we will not repeat this.

Use the following code to determine if it is unicode/str:

<pre name= "code" class= "python" >-*-coding=utf-8-*-isinstance (u ' haha hello ', Unicode) isinstance (' Haha hello ', str)

The print results are true.

Utf-8 or GBK?

Both of these are Chinese encoding, choose which one can be, but note that the same project must be unified use, not mixed.

Header declaration Coding=utf-8, a = ' Chinese ' whose encoding is utf-8

If the head is declared coding=gb2312, a = ' Chinese ' is encoded as GBK

All source files in the same project have a single code header, and the encoding to be declared is consistent with the encoding of the source file (Editor-related)

PS: Hard-coded string used in source code for processing, unified Unicode

Isolate the encoding of its type from the source file itself, independent of the convenience of each location in the process.


Code Implementation

1. In the simplest case, output Chinese directly in the code line, that is, the Chinese string is not stored in the variable, as follows:

Print u ' haha hello '
Here is the Chinese string ' haha hello ' decoded into a Unicode string, if the direct print ' haha hello ' will show garbled

2. If the Chinese string is stored in a string variable, it is implemented as follows:

string= ' haha hello ' print string.decode (' UTF-8 ')
The principle is actually the same as the first case, the Chinese string ' haha hello ' decoded into a Unicode string

3. A bit more complicated, Chinese string is stored in a tuple, list or dictionary should be how to output it? Because only the Str object can call the Decode method, Tuple/list/dict call decode will error, perhaps you would say to convert these objects to STR object and then call decode, but that is cumbersome and inefficient program. You can invoke the dumps method of the JSON module, as follows:

tuple= (' haha ', ' hello ') list=[' haha ', ' hello ']dict={1: ' haha ', 2: ' Hello '}print json.dumps (tuple,encoding= ' UTF-8 ', ensure_ascii=false ) Print json.dumps (list,encoding= ' UTF-8 ', ensure_ascii=false) print json.dumps (dict,encoding= ' UTF-8 ', ensure_ascii= False)
Be aware of the two parameters in the Dumps method

Complete code and print results

#-*-Coding:utf-8-*-## #测试python输出中文是否乱码import jsonprint u ' haha hello ' string= ' haha hello ' print string.decode (' UTF-8 ') tuple= (' Haha ', ' hello ') list=[' haha ', ' hello ']dict={1: ' haha ', 2: ' Hello '}print json.dumps (tuple,encoding= ' UTF-8 ', ensure_ascii=false) print Json.dumps (list,encoding= ' UTF-8 ', ensure_ascii=false) print json.dumps (dict,encoding= ' UTF-8 ', ensure_ascii=false)
Run the Python program in the Windows console with the following output:

5 kinds of Chinese output can be displayed without garbled characters.

About Python output Chinese there are a lot of primitive things to understand, and the compiler platform and Python version may be different, but the general method is similar.




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Python output Chinese--How to print in Windows console is not garbled

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.