0

So im reading from a file like so.

f = open("log.txt", 'w+r')
f.read()

So f now has a bunch of lines, but im mainly concerned with it having a number, and then a specific string (For example "COMPLETE" would be the string)

How...exactly would you go about checking this?
I thought it'd be something like:

r.search(['[0-9]*'+,"COMPLETE")

but that doesn't seem to work? Maybe it's my Regex thats wrong (im pretty terrible at it) but basically it just needs to check the Entire String (which is multiple lines and contain's \n's for a Number (specifically 200) and the word COMPLETE (in caps)

edit: For reference here is what the logfile looks like

Using https
Sending install data... DONE
200 COMPLETE
<?xml version="1.0" encoding="UTF-8"?>
<SolutionsRevision version="185"/>

I just need to make sure it says "200" and COMPLETE

2
  • You didn't quote your regex, first off. Some sample data would be helpful. Commented May 18, 2011 at 16:39
  • 2
    Can you post an example of the lines in log.txt? Commented May 18, 2011 at 16:39

3 Answers 3

5

Regular expressions are overkill here if you're just looking for "200 COMPLETE". Just do this:

if "200 COMPLETE" in log:
    # Do something
Sign up to request clarification or add additional context in comments.

Comments

1

You should use Rafe's answer instead of regex to search for "200 COMPLETE" in the file content, but your current code won't work for reading the file, using "w+r" as the mode will truncate your file. You need to do something like this:

f = open("log.txt", "r")
log = f.read()
if "200 COMPLETE" in log:
    # Do something

Comments

1

It should be something like

m = r.search('[0-9]+\s+COMPLETE',line)

1 Comment

line is the line(s) from the file. You need a source to look at right??

Your Answer

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