python 脚本中 # -*- coding: utf-8 -*-的作用
001、
# -*- coding: utf-8 -*-的作用是告诉python编码,默认的是ASCII编码,使用中文注释的情况下会报错,而增加# -*- coding: utf-8 -*-则程序可以正常执行
[root@pc1 test1]# ls ## 下面是两个测试程序 test01.py test02.py [root@pc1 test1]# cat test01.py ## test01.py程序不包括 # -*- coding: utf-8 -*-, 同时给与中文注释,观察程序运行效果 #!/usr/bin/env python # 注释内容 list1 = ["aa", "bb", "cc"] print(list1) [root@pc1 test1]# cat test02.py ## test02.py程序包括 # -*- coding: utf-8 -**, 同时给与中文注释,观察程序运行效果 #!/usr/bin/env python # -*- coding: utf-8 -*- # 注释内容 list1 = ["aa", "bb", "cc"] print(list1) [root@pc1 test1]# python test01.py ## test01.py程序报错 File "test01.py", line 3 SyntaxError: Non-ASCII character '\xe6' in file test01.py on line 3, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details [root@pc1 test1]# python test02.py ## test02.py程序运算正常 ['aa', 'bb', 'cc']

。
以上情况只适用与python2.

浙公网安备 33010602011771号