【python】解决sys:1: DeprecationWarning:错误提示

简介: 在较早的python版本中,由于默认Python只支持显示ascii码,在不加提示的情况下,py脚本中有中文时,会出现如下错误[yang@rac1 python]$ vim 2-10.
在较早的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]$ 
目录
相关文章
|
数据采集 数据挖掘 编译器
【Python 基础教程】错误与异常的处理
【Python 基础教程】错误与异常的处理
【Python 基础教程】错误与异常的处理
|
存储 缓存 安全
【python】错误和异常(第三讲)
assert,翻译过来是“断言”之意。assert 是一句等价于布尔真的判定,发生异常就意味着表达式为假。 assert 的应用情景就有点像汉语的意思一样,当程序运行到某个节点的时候,就断定某个变量的值必然是什么,或者对象必然拥有某个属性等,简单说就是断定什么东西必然是什么,如果不是,就抛出错误。......
365 0
【python】错误和异常(第三讲)
|
自然语言处理 Linux 测试技术
【python】错误和异常(第二讲)
处理多个异常,并不是因为同时报出多个异常。程序在运行中,只要遇到一个异常就会有反应,所以,每次捕获到的异常一定是一个。所谓处理多个异常的意思是可以容许捕获不同的异常,有不同的 except 子句处理。......
246 0
【python】错误和异常(第二讲)
|
Linux 云计算 索引
【python】错误和异常(第一讲)
逻辑错误可能会由于不完整或者不合法的输入导致,也可能是无法生成、计算等,或者是其它逻辑问题。当 Python 检测到一个错误时,解释器就无法继续执行下去,于是抛出异常。
396 0
【python】错误和异常(第一讲)
|
Python
Python错误:'tuple' object is not callable
Python错误:'tuple' object is not callable
540 0
|
程序员 Python Windows
全网首发:安装Python(或其他软件)出现0x80072f7d错误的解决办法
全网首发:安装Python(或其他软件)出现0x80072f7d错误的解决办法
530 0
|
监控 Python
Python-异常与错误
Python-异常与错误
434 0
Python-异常与错误
|
数据格式 Python
Python常见问题 - 写入数据到 excel 报 ValueError: invalid literal for int() with base 10 错误
Python常见问题 - 写入数据到 excel 报 ValueError: invalid literal for int() with base 10 错误
536 0
Python常见问题 - 写入数据到 excel 报 ValueError: invalid literal for int() with base 10 错误
|
测试技术 Python
Python常见问题 - 使用openpyxl模块时出现错误: zipfile.BadZipFile: File is not a zip file
Python常见问题 - 使用openpyxl模块时出现错误: zipfile.BadZipFile: File is not a zip file
2213 0
Python常见问题 - 使用openpyxl模块时出现错误: zipfile.BadZipFile: File is not a zip file
|
IDE 测试技术 开发工具
Python(10)错误、调试、测试(下)
Python(10)错误、调试、测试(下)
243 0

推荐镜像

更多