0

I have a huge log file which I want to parse using python. The file contains the data of working and non working audio/video/image file

The content of log.txt

filename = abc.mp4
played correctly

filname = cdf.wav
failed

filename = rtg.mp3
failed

Now from this log.txt how to find out failed filename I tried re.search but seems like I not reaching at any point with that approach So I used following approach but so far I am not able the way to find out the filename

f = open('log.txt', 'r')
for i in f.realines():
    if "failed" in i:
        print "failed files: "
f.close()

2 Answers 2

3

Save a reference to the filename temporarily, then add it to a list if it failed:

with open('log.txt') as f:
    result = []
    for line in f:
        line = line.strip()
        if not line:
            continue
        if '=' in line:
            name = line.split('= ')[1]
        if line == 'failed':
            result.append(name)

After the above, result will be a list with all the failed filenames.

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

Comments

0

You can simply modify your code by adding a dump variable.

In[6]: f = open('test', 'r')
for i in f.readlines():
    if "failed" in i :
        print "failed files: ", dump.split('= ')[1]
    dump = i
f.close()

Output
failed files:  cdf.wav
failed files:  rtg.mp3

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.