1

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 'EXAMPLE LINE IN LOG FILE WITH extra info tag=on' in line:              
   output_file.write(datestring + " Device was turned on" + "\n")

Not very pythonic but it works for me.

Now what I want to do is write Divices was turned off if the line in the log says tag=off. I can simply copy the code again like this:

if 'EXAMPLE LINE IN LOG FILE WITH extra info tag=off' in line:              
   output_file.write(datestring + " Device was turned off" + "\n")

But that is not very nice and it is more easy editing in the future.

Can someone help my on my way?

3 Answers 3

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/

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for taking the time.
0

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.

7 Comments

Thank you for taking the time.
I think I should be more specific. The log file I want to pase is a Whatsapp logfile. And the logline i want to parse is: ScreenLockReceiver; tag=on; locked=true; oldLocked=true. Now the locked and oldLocked I dont care about. But the tag can have 3 values. on, off or unlocked.
Can you provide examples? You can replace any sensitive information, we just need to see what a line can look like.
A line can look like this: ScreenLockReceiver; tag=on; locked=true; oldLocked=true. Where the tag= can be on off or unlocked.
Then what Alfie and I suggested should work. Try either code and accept the corresponding answer if it works.
|
0

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.