在较早的python版本中,由于默认Python只支持显示ascii码,在不加提示的情况下,py脚本中有中文时,会出现如下错误
[yang@rac1 python]$ vim 2-10.py
#!/etc/bin/python
list=[1,35,6,48,2,35,52]
sum= 0
average=0
for i in list:
sum += i
average = sum/7
print 'list 数组中的平均值为:',average
"2-10.py" [New] 11L, 156C written
[yang@rac1 python]$ python 2-10.py
sys:1: DeprecationWarning: Non-ASCII character '\xe6' in file 2-10.py on line 10, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
list 数组中的平均值为: 25
打开http://www.python.org/peps/pep-0263.html,可以找到解决方法。
在脚本中添加# -*- coding:utf8 -*- ,python就可以支持中文了。
[yang@rac1 python]$ vim 2-10.py
#!/etc/bin/python
# -*- coding:utf8 -*-
list=[1,35,6,48,2,35,52]
sum= 0
average=0
for i in list:
sum += i
average = sum/7
print 'list 数组中的平均值为:',average
"2-10.py" 11L, 178C written
[yang@rac1 python]$ python 2-10.py
list 数组中的平均值为: 25
[yang@rac1 python]$