0

I have log file in wich at some point I added timestam creation and now it look like this:
log.txt:

327555
327563
327570
327601
2012-11-19 22:21:37 :: 327001
2012-11-19 22:21:37 :: 327004
2012-11-19 22:21:37 :: 327007
2012-11-19 22:21:37 :: 327008

In my Python script i used to read all lines from log.txt and add it line-by-line to a set for futher usage:

log_file = open('log.txt')
set_log = set([])
for line in log_file:
    set_log.add(line.strip())
log_file.close()

But since timestam was added this solution and gives me wrong values in my set ( it includes timestam as well).

Q: How to make it more flexible, so it udersteads lines without timestams and WITH timestams and extract only proper values?

1 Answer 1

2

Just parse away the timestamps. ln.split()[-1] will return the last element after splitting on whitespace, which seems to be what you're after, so

set_log = set(ln.split()[-1] for ln in log_file)

(Using a generator comprehension to replace your loop. The strip() is no longer needed as split() removes all whitespace.)

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

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.