简体   繁体   English

如何将此 function 简化为检查列表中所有错误代码的 function?

[英]How can I simplify this function into a function that checks all error codes from a list?

My problem is that I have a many functions that essentially do the same thing depending on a given error code.我的问题是我有许多函数,它们基本上根据给定的错误代码做同样的事情。 How can I develop a loop to simplify these functions into one algorithm that checks the error code from a list or table of error codes.如何开发一个循环以将这些函数简化为一种算法,该算法从错误代码列表或表格中检查错误代码。 As well as detecting the error code string, I would like to count occurrences for each unique error code.除了检测错误代码字符串外,我还想计算每个唯一错误代码的出现次数。

I've tried to make a table that includes column such as error and description.我试图制作一个包含错误和描述等列的表格。 I am confused how I can match the error with the description我很困惑如何将错误与描述相匹配

file_path = ('/Users/.../error.txt')


def check_error1():

    count = 0
    fault = ':error1'

    with open(file_path) as f:
        for line in f.readlines():
            if fault in line:
                count += fault.count(fault)
    return count != 0

if check_error1():
    print 'error 1 occurred ' , count, ' times || WARNING, Bot failed to read data'

print 'error 1 occurred ' , count, ' times || WARNING, Bot failed to read data'

You could find all faulty lines in the file and then take the len gth of that:您可以在文件中找到所有错误行,然后获取其中的len

def check_error1():
    with open(file_path) as f:
        return len(line for line in f if ':error1' in line)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM