[Tutor] returning the entire line when regex matches
Nick Burgess
burgess.nick at gmail.com
Mon May 4 17:57:48 CEST 2009
So far the script works fine, it avoids printing the lines i want and
I can add new domain names as needed. It looks like this:
#!/usr/bin/python
import re
outFile = open('outFile.dat', 'w')
log = file("log.dat", 'r').read().split('Source') # Set the line delimiter
for line in log:
if not re.search(r'notneeded.com|notneeded1.com',line):
outFile.write(line)
I tried the in method but it missed any other strings I put in, like
the pipe has no effect. More complex strings will likely be needed so
perhaps re might be better..?
the next task would be to parse all files in all subdirectories,
regardless of the name of the file as the file names are the same but
the directory names change
I have been playing with os.walk but im not sure if it is the best way.
for root, dirs, files in os.walk
I guess merging all of the files into one big one before the parse
would work but I would need help with that too.
the tutelage is much appreciated
-nick
On Sun, May 3, 2009 at 6:21 PM, Alan Gauld <alan.gauld at btinternet.com> wrote:
>
> "Alan Gauld" <alan.gauld at btinternet.com> wrote
>
>>> How do I make this code print lines NOT containing the string 'Domains'?
>>>
>>
>> Don't use regex, use in:
>>
>> for line in log:
>> if "Domains" in line:
>> print line
>
> Should, of course, be
>
> if "Domains" not in line:
> print line
>
> Alan G.
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
More information about the Tutor
mailing list