python outputs Chinese garbled characters

 

 

Since the Python source code is also a text file, when your source code contains Chinese, you need to specify the UTF-8 encoding when saving the source code. When the Python interpreter reads the source code, in order to make it read in UTF-8 encoding, we usually write these two lines at the beginning of the file:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

The first line of comment is to tell the Linux/OS X system that this is a Python executable program, and the Windows system will ignore this comment;

The second line of comment is to tell the Python interpreter to read the source code according to UTF-8 encoding, otherwise, the Chinese output you wrote in the source code may have garbled characters.

Declaring UTF-8 encoding does not mean your .pyfile is UTF-8 encoded, you must and make sure your text editor is using UTF-8 without BOM encoding

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325096186&siteId=291194637