Python Regex on File Read Input
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 i开发者_JS百科t 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
Regular expressions are overkill here if you're just looking for "200 COMPLETE". Just do this:
if "200 COMPLETE" in log:
# Do something
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
It should be something like
m = r.search('[0-9]+\s+COMPLETE',line)
精彩评论