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 ~]# 
  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值