Recently developed and learned Pyton. When adding Chinese comments, the running program reported an error:
File "red.py", line 10 SyntaxError: Non-ASCII character '\xe5' in file red.py on line 10, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
Solution:
In the Python script file Add a sentence to the first or second line:
# -*- coding: utf-8 -*-
Since the Python source code is also a text file, when your source code contains Chinese, when saving the source code, Be sure to specify saving as UTF-8 encoding. When the Python interpreter reads the source code, in order for it to be read in UTF-8 encoding, we usually write these two lines at the beginning of the file:
#!/usr/bin/env python //为了告诉Linux系统,这个是python可执行程序 # _*_ coding:utf-8 _*_ //为了告诉python解释器,按照utf-8编码读取源代码,否则,你在源代码中写的中文输出可能会由乱码 #coding=utf-8 //上一行代码也可以写成这种形式
For more Python-related technical articles, please visit PythonTutorial column to learn!
The above is the detailed content of How to add Chinese comments when programming in python. For more information, please follow other related articles on the PHP Chinese website!