Python 中文注释报错解决方案

简介:

今天在学习python的时候注释了一条中文,结果报错:

File “test.py”, line 3
SyntaxError: Non-ASCII character ‘\xe8’ in file test.py on line 3, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

大致意思是说,文件 ‘test.py’ 第三行有问题,是语法错误,并没有ASCII字符,也没有编码声明,请看python官网。

Defining the Encoding
    Python will default to ASCII as standard encoding if no other
    encoding hints are given.

    To define a source code encoding, a magic comment must
    be placed into the source files either as first or second
    line in the file, such as:

          # coding=<encoding name>
          # -*- coding: <encoding name> -*-

官网中大致说:
Python默认使用ASCII标准编码,如果没有其他编码提示,要定义一个源代码编码,下面的格式大家也都看到了,是 coding= encoding name

后来我在 ‘test.py’ 中添加了一行 ‘# -*- coding: utf-8 -*-‘,之后就没有报错了。

[root@hongxue_216 ~]# cat test.py 
#!/usr/bin/python
# -*- coding:utf8 -*-

# 这是注释
print "Hello World!"
[root@hongxue_216 ~]# 
[root@hongxue_216 ~]# ./test.py 
Hello World!
[root@hongxue_216 ~]# 
相关文章
|
2月前
|
设计模式 测试技术 Python
游戏开发的挑战与解决方案:Python 视角
游戏开发是一个复杂且富有挑战性的过程,涉及到多个领域的知识和技术。从 Python 的角度来看,游戏开发面临着一系列的挑战,同时也有许多解决方案可供选择。
|
3月前
|
Web App开发 Python
Python使用selenium的Chrome下载文件报错解决
Python使用selenium的Chrome下载文件报错解决
46 0
|
3月前
|
数据采集 Python
【Python自动化】多线程BFS站点结构爬虫代码,支持中断恢复,带注释
【Python自动化】多线程BFS站点结构爬虫代码,支持中断恢复,带注释
28 0
|
2月前
|
安全 网络安全 API
python调用openai api报错self._sslobj.do_handshake()OSError: [Errno 0] Error
python调用openai api报错self._sslobj.do_handshake()OSError: [Errno 0] Error
69 1
python调用openai api报错self._sslobj.do_handshake()OSError: [Errno 0] Error
|
2月前
|
数据可视化 数据挖掘 调度
【Python数据挖掘】优化电能能源策略:基于非侵入式负荷检测与分解的智能解决方案
【Python数据挖掘】优化电能能源策略:基于非侵入式负荷检测与分解的智能解决方案
34 0
|
26天前
|
算法 程序员 C++
【Python 基础教程 05】超详细解析Python3注释:全面入门教程,初学者必读,了解Python如何 进行注释
【Python 基础教程 05】超详细解析Python3注释:全面入门教程,初学者必读,了解Python如何 进行注释
87 1
|
1月前
|
存储 数据处理 调度
Python实现定时任务的方案及其比较
Python实现定时任务的方案及其比较
26 0
|
1月前
|
安全 Python
Python在不同场景下的并发编程方案选择
Python在不同场景下的并发编程方案选择
15 0
|
1月前
|
Java 编译器 C语言
python基础语法——常量、变量、注释、输入输出和运算符
本文基于pycharm编译器,也可以使用Anaconda 里的编译器,将讲解一些python的一些基础语法知识。可以和我写的python数据分析——Python语言基础(语法基础)结合起来看,有些知识点可能在这篇文章写的不是很全面
17 0