Search code examples
pythonregexlogfile

Extracting a particular text from a log file using regex


I have the following log file

2020-06-30 12:44:06,608 DEBUG [main] [apitests.ApiTest] Reading of Excel File Started
2020-06-30 12:44:11,853 DEBUG [main] [apitests.ApiTest] The Keyword's Entered : Asus Laptop
2020-06-30 12:44:11,853 DEBUG [main] [apitests.ApiTest] No of Keywords Entered = 1
2020-06-30 12:44:11,853 DEBUG [main] [apitests.ApiTest] Response Code from API : 200
2020-06-30 12:44:11,853 DEBUG [main] [apitests.ApiTest] Time Taken : 1959 milliseconds
2020-06-30 12:44:11,853 DEBUG [main] [apitests.ApiTest] The Result Obtained from API is : {"keywords": {"Asus Laptop": ["Premium grade"]}}
2020-06-30 12:44:11,853 DEBUG [main] [apitests.ApiTest] --------------------------------------------------------------------------------------
2020-06-30 12:44:12,136 DEBUG [main] [apitests.ApiTest] The Keyword's Entered : Intext Hardrive
2020-06-30 12:44:12,136 DEBUG [main] [apitests.ApiTest] No of Keywords Entered = 1
2020-06-30 12:44:12,136 DEBUG [main] [apitests.ApiTest] Response Code from API : 200
2020-06-30 12:44:12,136 DEBUG [main] [apitests.ApiTest] Time Taken : 243 milliseconds
2020-06-30 12:44:12,136 DEBUG [main] [apitests.ApiTest] The Result Obtained from API is : {"keywords": {"Intext Hardrive": ["Medium grade"]}}
2020-06-30 12:44:12,136 DEBUG [main] [apitests.ApiTest] --------------------------------------------------------------------------------------

My goal is to just extract the words ["premium grade"], ["Medium grade"]....and so on. Basically the value of the key value.

I wrote the below code.

import re
with open('quality.log', 'r') as text_file:
    text_file=text_file.read()  
    for line in text_file :  
        matches=re.findall(r"\[(.*?)\]", line)[0]
with open('qualitygrade.txt', 'w') as out:
    out.write('\n'.join(matches))

The goal of the re.findall(r"\[(.*?)\]", line)[0] is to just extract the "premium grade","medium grade" etc.

Not sure what I am doing wrong. My outputtext is blank. Any help pls.


Solution

  • You don't need the for loop as you are reading the whole file at once.

    Your code could be:

    with open('quality.log', 'r') as text_file:
        text_file=text_file.read()
        matches = re.findall(r'\["(.*?)"]', text_file)
    

    If you want to get the values between the double quotes, you should add them to the pattern.

    \["(.*?)"]
    

    Output

    Premium grade
    Medium grade