Skip to content Skip to sidebar Skip to footer

Log Parsing With Python

I am a beginner in python. What I want to do is to make a log file readable. For now what I have is a way to read and write a file.. For parsing the log line I use this code: if 'E

Solution 1:

As mentioned in Alexandre's answer, if every line includes tag=on or tag=off then a simple if-else should do the job:

file_string = datestring + " Device was turned" 
if "tag=on" in line:
    file_string += " on\n"
else:
    file_string += " off\n"
output_file.write(file_string)

You can also tidy your code up a little using f-strings:

file_string = f"{datestring} Device was turned" 
if "tag=on" in line:
    file_string += " on\n"
else:
    file_string += " off\n"
output_file.write(file_string)

Using f-strings is a way to quickly concatenate loads of different variables in a string, just be sure to place any variables inside of the {} characters. This has the added benefit of automatically converting anything to string-type.

You mentioned that you're a beginner, so something else you may find helpful is using a context manager to handle your file operations.

output_file = open('output_file.txt', 'w')
output_file.write(file_string)
output_file.close()

Is the same as:

with open('output_file.txt', 'w') as f:
    f.write(file_string)

Using a context manager will automatically close the files for you, and has the added benefit of making your code look much cleaner. Putting everything together:

with open('output_file.txt', 'w') as f:
    for line in lines:
        file_string = f"{datestring} Device was turned" 
        if "tag=on" in line:
            file_string += " on\n"
        else:
            file_string += " off\n"
    output_file.write(file_string)

Hopefully that helps! I've written an article on opening and closing files properly if you'd like to learn more:

https://www.learndatasci.com/solutions/python-close-file/

I've also got another one which explains f-strings in more detail (see the solution at the end of cause 1):

https://www.learndatasci.com/solutions/python-typeerror-can-only-concatenate-str-not-int-str/


Solution 2:

If every line of your log contains either tag=on or tag=off, you could do this:

output_file.write(datestring + " Device was turned " + "on" if "tag=on" in line else "off" + "\n")

The if is now directly inside the write() statement and dynamically changes depending on the line.

Edit:

Since you can have multiple values for tag, you can no longer only use if/else as I suggested.

What you could do now is check for the value of tag with Alfie's solution using an elif statement.


Solution 3:

Okay so I kept this one simple and easy for me to understand:

        if 'ScreenLockReceiver; tag=on' in line:              
           output_file.write(datestring + " Screen was activated." + "\n")
        if 'ScreenLockReceiver; tag=off' in line:              
           output_file.write(datestring + " Screen was deactivated." + "\n")
        if 'ScreenLockReceiver; tag=unlock' in line:              
           output_file.write(datestring + " Device was unlocked." + "\n")

Thanks for the help! Understanding a bit more!


Post a Comment for "Log Parsing With Python"