简体   繁体   中英

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
Line: 6, Error: No such member|>>>>>>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.
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. -> it will be discard before temp string. you should recognize it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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