python output_Python警告消息输出(Python warning message output)

本文介绍了如何在Python中控制警告消息的输出,特别是如何去除警告信息中的多余行,仅显示关键的警告内容。示例代码展示了如何使用`warnings.warn()`函数的`stacklevel`参数来定位警告产生的位置,以便用户能更清晰地看到问题所在。
摘要由CSDN通过智能技术生成

Python警告消息输出(Python warning message output)

使用此代码:

#! /usr/bin/env python

# -*- coding: utf-8 -*-

import sys

import warnings

if sys.version_info[0] >= 3:

...

else:

warnings.warn("Python 3.x is required!", RuntimeWarning)

我得到的else输出是:

Warning (from warnings module):

File "C:\Users\..., line 10

warnings.warn("Python 3.x is required!", RuntimeWarning)

RuntimeWarning: Python 3.x is required!

有没有办法摆脱输出的前3行,只显示“RuntimeWarning:Python 3.x是必需的!” ?

With this code:

#! /usr/bin/env python

# -*- coding: utf-8 -*-

import sys

import warnings

if sys.version_info[0] >= 3:

...

else:

warnings.warn("Python 3.x is required!", RuntimeWarning)

And the else output I get is:

Warning (from warnings module):

File "C:\Users\..., line 10

warnings.warn("Python 3.x is required!", RuntimeWarning)

RuntimeWarning: Python 3.x is required!

Is there any way of getting rid of the first 3 lines of the output and only display "RuntimeWarning: Python 3.x is required!" ?

原文:https://stackoverflow.com/questions/41269618

2020-09-29 11:09

满意答案

警告中的堆栈级别

您会注意到,默认情况下,警告消息包含生成它的源行(如果可用)。 但是,使用实际警告消息查看代码行并不是那么有用。 相反,你可以告诉warn()它必须走多远才能找到被称为包含警告的函数的行。 这样,不推荐使用的函数的用户可以看到调用函数的位置,而不是函数的实现。

# warnings_warn_stacklevel.py

import warnings

def old_function():

warnings.warn(

'old_function() is deprecated, use new_function() instead',

stacklevel=2)

def caller_of_old_function():

old_function()

caller_of_old_function()

请注意,在此示

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
-- coding: utf-8 -- import arcpy import os import sys reload(sys) sys.setdefaultencoding('utf-8') def check_feature_class_fields(workspace, output_file): def write_warning_to_file(warning_message): with open(output_file, 'a') as f: f.write(warning_message + '\n') arcpy.env.workspace = workspace feature_classes = arcpy.ListFeatureClasses() unqualified_count = 0 field_count = {} for feature_class in feature_classes: feature_class_path = os.path.join(workspace, feature_class) fields = arcpy.ListFields(feature_class_path) field_names = [field.name for field in fields] with arcpy.da.SearchCursor(feature_class_path, ["OID@"] + field_names) as cursor: for row in cursor: oid = row[0] for i, value in enumerate(row[1:], start=1): if value is None or value == "": field_name = field_names[i - 1] warning_message = "Unqualified Feature Class: {} | Field Name: {} | Feature OID: {}".format( feature_class, field_name, oid ) arcpy.AddWarning(warning_message) write_warning_to_file(warning_message) unqualified_count += 1 if field_name in field_count: field_count[field_name] += 1 else: field_count[field_name] = 1 # Write the total count of unqualified cases to the output file. with open(output_file, 'a') as f: f.write("Total Unqualified Cases Found: {}\n".format(unqualified_count)) f.write("Field-wise Unqualified Case Count:\n") for field_name, count in field_count.items(): f.write("Field: {} | Count: {}\n".format(field_name, count)) workspace = arcpy.GetParameterAsText(0) output_file_path_txt = "unqualified_data.txt" check_feature_class_fields(workspace, output_file_path_txt) 检查上述代码是否使用Python 2语言
06-13

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值