Python read log files and get lines containing specific words

后端 未结 2 1887
一整个雨季
一整个雨季 2020-12-30 13:50

I have log files ( named in the format YYMMDD ) and I\'d like to create a script that get only important information from the files ( like the lines that contains \"O:NVS:V

2条回答
  •  無奈伤痛
    2020-12-30 14:23

    This should get you started nicely:

    infile = r"D:\Documents and Settings\xxxx\Desktop\test_log.txt"
    
    important = []
    keep_phrases = ["test",
                  "important",
                  "keep me"]
    
    with open(infile) as f:
        f = f.readlines()
    
    for line in f:
        for phrase in keep_phrases:
            if phrase in line:
                important.append(line)
                break
    
    print(important)
    

    It's by no means perfect, for example there is no exception handling or pattern matching, but you can add these to it quite easily. Look into regular expressions, that may be better than phrase matching. If your files are very big, read it line by line to avoid a MemoryError.

    Input file:

    This line is super important!
    don't need this one...
    keep me!
    bla bla
    not bothered
    ALWAYS include this test line
    

    Output:

    ['This line is super important!\n', 'keep me!\n', 'ALWAYS include this test line']
    

    Note: This is Python 3.3.

提交回复
热议问题