简体   繁体   English

python 解析日志文件:在不同的行中找到两个特定的字符串并连接到一个并写入另一个文件! 避免空行

[英]python parse log file: find two specific strings in different lines and concatenate in one and write to another file! Avoiding blank lines

My log like this file我的日志喜欢这个文件

what I need to look like:我需要看起来像什么:

Line: 5, Error: The member of the Account dimension is not base level.|>>>>>>A1399行:5,错误:帐户维度的成员不是基本级别。|>>>>>>A1399
Line: 6, Error: No such member|>>>>>>401700行:6,错误:没有这样的成员|>>>>>>401700

I need to parse a log file and find only the errors that are of interest and write them to another file.我需要解析一个日志文件,只找到感兴趣的错误并将它们写入另一个文件。 Below is the code using the two tags ( 'Line:','>>>>>>' ) I need to get all the strings after these two tags and concatenate in one new line.下面是使用两个标签( 'Line:','>>>>>>' )的代码我需要获取这两个标签之后的所有字符串并连接到一个新行中。
I get a lot of empty lines and the two tags are in different lines.我得到很多空行,两个标签在不同的行中。
Thank you in advance!先感谢您!

def main():
    fo = open("C:\\Users\\yannis\\py_script\\1198.log", "r", encoding="ISO-8859-1")
    ofile = open("C:\\Users\\yannis\\py_script\\out.txt",'a', newline='')
    member = ""
    erro = ""

    f1 = fo.readlines()
    for x in f1:
        erro = (x[x.find('Line:'):])
        member = (x[x.find('>>>>>>'):])

        linha = (erro + member)
        print(linha)
        ofile.write(linha)
        continue




    fo.close()
    ofile.close()



main()
def log_parser(path):
    with open(path, 'r', encoding='utf-8') as f:
        temp = dict()
        for index, line in enumerate(f.readlines()):
            _line_except = line[:-1]
            [day, time, level, something, *arg] = _line_except.split(' ')

            if 'Error:' in arg and 'Line:' in arg:
                temp = {
                    "log_info": " ".join([day, time, level, something]),
                    "log_str": " ".join(arg)
                }
            elif len(arg) == 1:
                print("{log_info} {log_str} || {other_args}".format(
                    log_info=temp['log_info'],
                    log_str=temp['log_str'],
                    other_args=arg[0])
                )
            else:
                continue


if __name__ == '__main__':
    log_parser("log.txt")

AND you will get..你会得到..

"""
OUTPUT : 
2020-07-03 15:21:58,962 ERROR [AIF]: Line: 5, Error: The member of the Account dimension is not base level. || >>>>>>A1399
2020-07-03 15:21:58,962 ERROR [AIF]: Line: 6, Error: No such member || >>>>>>401700
2020-07-03 15:21:58,962 ERROR [AIF]: Line: 7, Error: The member of the Account dimension is not base level. || >>>>>>A1399
2020-07-03 15:21:58,962 ERROR [AIF]: Line: 15, Error: The member of the Account dimension is not base level. || >>>>>>A2090
2020-07-03 15:21:58,962 ERROR [AIF]: Line: 16, Error: The member of the Account dimension is not base level. || >>>>>>A2090
2020-07-03 15:21:58,962 ERROR [AIF]: Line: 26, Error: No such member || >>>>>>101020
2020-07-03 15:21:58,962 ERROR [AIF]: Line: 27, Error: No such member || >>>>>>10132
"""

there is something err will be occured, when you don't write ">>>>12345" in log file.当您不在日志文件中写入“>>>>12345”时,将会发生错误。 -> it will be discard before temp string. -> 它将在临时字符串之前被丢弃。 you should recognize it.你应该认出它。

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

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