计算机程序有时很人性化,比如给你警告提示信息;

计算机程序有时又非常不人性化,比如动不动就给你警告提示......

如果你的程序是要给客户使用,有运行美化要求;

再尤其是比如警告出现在循环里的情况,那么每次循环都要打印出一堆警告信息......那就十分扯淡了。

怎么办?

显然,警告(Warning)并不是错误(Error),程序并不会因警告的出现而中止运行。

那些原始开发者一定早已考虑到这一点,设置了可供调整的警告输出模式。

我们以Python和R为例,看看基本的警告信息管理操作。

(一)Python

我们编写以下程序:

import warnings as w
if 1==1:
w.warn('警告!!!')

运行结果为:

Warning (from warnings module):
File "D:/warnings.py", line 3
w.warn('警告!!!')
UserWarning: 警告!!!

1)当使用命令行模式执行程序时,可在执行命令里加入-W ignore:

python -W ignore XXX.py

此时运行结果不输出警告。

2)也可在程序中导入warnings模块,使用警告过滤器。

import warnings as w
w.filterwarnings("ignore")
if 1==1:
w.warn('警告!!!')

此时运行结果也不输出警告。

而事实上,还有其它几个参数可供选择以控制警告:

Value

Disposition

"error"

turn matching warnings into exceptions

"ignore"

never print matching warnings

"always"

always print matching warnings

"default"

print the first occurrence of matching warnings for each location where the warning is issued

"module"

print the first occurrence of matching warnings for each module where the warning is issued

"once"

print only the first occurrence of matching warnings, regardless of location

如将程序改为:

import warnings as w
w.filterwarnings("ignore")
if 1==1:
w.warn('警告!!!')
w.filterwarnings("always")
if 1==1:
w.warn('警告!!!---')
w.filterwarnings("error")
if 1==1:
w.warn('警告!!!---===')
print('sfsdfsfsdfsdf')

输出结果:

Warning (from warnings module):
File "D:/warnings.py", line 7
w.warn('警告!!!---')
UserWarning: 警告!!!---
Traceback (most recent call last):
File "D:/warnings.py", line 10, in <module>
w.warn('警告!!!---===')
UserWarning: 警告!!!---===

(二)R

R语言控制警告就更方便了。可在程序前插入命令:

options(warn=N)

其中,N的取值可以为负数、0、1、2。

warn = 负数,则所有warning message都被忽略。
warn = 0 (默认值),则所有warning messages会被储存起来直到上级函数运行结束。
warn = 1,则一旦产生warning message,这条信息会被立即显示出来。
warn = 2 或更大的数值, 则warning message会被立即显示并转换成error message。

参考资料:

https://docs.python.org/2/library/warnings.html#temporarily-suppressing-warnings

https://blog.csdn.net/xiaodongxiexie/article/details/65646239

https://blog.csdn.net/stat_elliott/article/details/37878247

Python&R:警告信息管理的更多相关文章

  1. VS code 配置为 Python R LaTeX IDE

    VS code配置为Python R LaTeX IDE VS code的中文断行.编辑功能强大,配置简单. VSC的扩展在应用商店搜索安装,快捷键ctrl+shift+x调出应用商店. 安装扩展后, ...

  2. 配置 Sublime Text 3 作为Python R LaTeX Markdown IDE

    配置 Sublime Text 3 作为Python R LaTeX Markdown IDE 配置 Sublime Text 3 作为Python IDE IDE的基本功能:代码提醒.补全:编译文件 ...

  3. 机器学习算法 Python&R 速查表

    sklearn实战-乳腺癌细胞数据挖掘( 博主亲自录制) https://study.163.com/course/introduction.htm?courseId=1005269003&u ...

  4. ubuntu 执行Python脚本出现: /usr/bin/env: ‘python\r’: No such file or directory

    原因: #!/usr/bin/env python 在ubuntu会变成 #!/usr/bin/env python\r 而\r 会被shell 当成参数 所以出现:  /usr/bin/env: ‘ ...

  5. 建模分析之机器学习算法(附python&R代码)

    0序 随着移动互联和大数据的拓展越发觉得算法以及模型在设计和开发中的重要性.不管是现在接触比较多的安全产品还是大互联网公司经常提到的人工智能产品(甚至人类2045的的智能拐点时代).都基于算法及建模来 ...

  6. python '%r'或者'{!r}'的意思

    转载:https://blog.csdn.net/a19990412/article/details/80149112 这两个都是python的转译字符, 类似于%r, %d,%f >>& ...

  7. Python&R&量化 金融之路

    [ 分类 ]- 金融之路 - 闲云孤鹤(人生在世五十年,大千世界一瞬间,浮生若梦,仿佛间,幻境一场,生者无常,终须尽.) - CSDN博客 https://blog.csdn.net/robertso ...

  8. 去掉python的警告

    1.常规警告 import warnings warnings.filterwarnings("ignore") 2.安装gensim,在python中导入的时候出现一个警告: w ...

  9. kNN算法及其python&R实现

    iris数据集,这一教科书级别的数据,分类前不需要做任何数据预处理什么的,相当的理想!但请注意你自己的数据99%的可能需要做预处理. 下面分别用R语言和Python来实现iris数据集的分类: R语言 ...

随机推荐

  1. Kali Linux 2019.2使用华为源

    一.将默认的配置源注释掉 root@zinuo:~# vim /etc/apt/sources.list 注释: #deb http://http.kali.org/kali kali-rolling ...

  2. java初学者编译简单的计算机

    package com.yj.test; import java.awt.BorderLayout; import java.awt.Font; import java.awt.GridLayout; ...

  3. python 爬虫 目录

    爬虫 介绍 python 爬虫 urllib模块 python 爬虫 requests模块

  4. [xpath] text()和string()区别

    质区别 text()是一个node test,而string()是一个函数,data()是一个函数且可以保留数据类型.此外,还有点号(.)表示当前节点. 使用要点 XML例子: <book> ...

  5. 啃掉Hadoop系列笔记(01)-Hadoop框架的大数据生态

    一.Hadoop是什么 1)Hadoop是一个由Apache基金会所开发的分布式系统基础架构 2)主要解决,海量数据的存储和海量数据的分析计算问题. 3)广义上来说,HADOOP通常是指一个更广泛的概 ...

  6. 【Bell-Ford 算法】CLRS Exercise 24.1-4,24.1-6

    本文是一篇笔记,大部分内容取自 CLRS 第三版,第 24.1 节. Exercise 24.1-4 Modify the Bellman-Ford algorithm so that it sets ...

  7. ARM Cortex-M 系列 MCU 错误追踪库 心得

    一. 感谢CmBacktrace开源项目,git项目网站:https://github.com/armink/CmBacktrace 二. 移植CmBacktrace 2.1 准备好CmBacktra ...

  8. Centos8 安装ifconfig(net-tools.x86_64)

    情况: yum search net-tools.x86_64 没有找到结果 解决: 1.yum search net-tools 2. yum install -y net-tools.x86_64 ...

  9. linux 安装 python 最全教程

    环境:centos6.5 centos6.5 自带的 python 版本是 2.6.6,需要重新安装 2.7: centos7 自带的 python 版本是 2.7.5 基本操作 在安装新版本之前,一 ...

  10. php7.2.1 安装

    yum -y install wget openssl* gcc gcc-c++ autoconf libjpeg libjpeg-devel libpng libpng-devel freetype ...