Hi guys,

I'm making a program which reads all files in a folder with test data, these files are known as logfiles. With each read logfile, I then need to extract only the lines which have the keyword in them... for example 'PASSED'.

Here lies the problem, when you open these logfiles with notpad, the entire file comes up on one line! When you open them with Wordpad they display as they should... So there's a special character. I've found the special character and for arguments sake let's call it '£'.

My code at present prints the line with the keyword on it - which unfortunately is the entire logfile :(

So what I need your help with is the part of the code which will read the entire file, looking for the keyword, when it finds the keyword, I want the script to put into a variable the following 'xxxx xxxxx xxxx xx xxxx PASSED xx xxxx xxx' from the code which would have '£' either side of this extracted text.

I've only been coding since monday and I think I'm doing a great job so far. I know I have soooo much to learn, but I'm really enjoying what I'm doing.

Thanks,

Matt

PS. The reason I have not included any code is that it's on another laptop and the only problem I have in the whole problem is listed above. Once I have the program working, I'll post the lot so you guys can help me improve it (if you want).

Recommended Answers

All 5 Replies

Do

print('\n'.join(passed for passed in chunk.split('£') if 'PASSED' in passed))

(text is assumed to be in variable chunk, hope not passed lines have FAILED not NOT PASSED)

Thanks Tony,

I'll give this a shot.

With regards to hoping there are no previous instances of PASSED, I've got that covered. The word PASS or FAIL is used for tests as they are run and the end statement states whether ALL TESTS PASSED or ALL TESTS DID NOT PASS.

So again, thanks for this, I'll try it out and get back with the results!

Matt

Do print('\n'.join(passed for passed in chunk.split('£') if 'PASSED' in passed)) (text is assumed to be in variable chunk, hope not passed lines have FAILED not NOT PASSED)

Well I tried that Tony, but I don't think it's suitable as it was returning blank (no characters).

So here's my code and I'm working on a different workaround which rather than look for the end of line character (there are several), it looks for the word I'm searching for, gives me the index, then I can write another routine to work with the index so I can get the information from the lines I need.

I'm having problems with this approach though....

ERROR:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python26\lib\lib-tk\Tkinter.py", line 1410, in __call__
    return self.func(*args)
  File "C:\Python23\Lib\Matt.py", line 47, in passed_info
    print lineb.find("b", [1, [56 ]])
TypeError: slice indices must be integers or None or have an __index__ method

PART OF CODE WITH THE PROBLEM:

   x = "1"
   while x == "1":
       path = r"C:\\TEST"
       entries = [join(path, entry) for entry in listdir(path)]
       files = filter(isfile, entries)
       c = open(join("C:\\TEST", "SearchResult.txt"), "w")

       b = "ELAPSED"
       d = "TESTS"
       e = "DATE"


       for file in files:
               f = open(file, 'r')
               for line in f.readlines():
                   if b in line:
                      lineb = line
                      print lineb
                      print lineb.find("b", [1, [56 ]])
               f.close()

If I've missed out any information you may need, please just ask, my brain refuses to function further after the battering it's just had!

Thanks,

Matt

It should be

if b in line:
                          print line.find(b)

as the two statements are related.

It should be

if b in line:
                          print line.find(b)

as the two statements are related.

Thanks very much, I wasn't aware you could do this! It's all a learning curve I guess.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.