Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (16.4k points)
Let's say I have some log files (In the format YYMMDD), then I would like to create a script that will get only information that is important from the files (like the lines that contain "O:NVS:VOICE"). Please help me guys, Because I'm totally new to python.

1 Answer

0 votes
by (26.4k points)

Look at the following code:

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 in no way, shape, or forms great, for instance, there is no exception handling or pattern matching, however, you can add these to it without any problem. Look into regular expression, which might be superior to express coordinating. On the off chance that your documents are extremely huge, read it line by line to keep away from 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']

PS: This is Python 3.3

Want to learn more about Python, Come & Join: Python training course
For more details, refer to this video tutorial on,

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Dec 17, 2020 in Python by ashely (50.2k points)
0 votes
2 answers
0 votes
1 answer
asked Dec 24, 2020 in Python by ashely (50.2k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...