0

Hi,

I'm pretty new to python but thought I'd write something to read this log file to try and learn it. Ideally, it will read this log file, match a string and return everything between the "****" delimiters where the string was matched. I'm not 100% sure how to go about this though. I populate my tuple with the following, but can't see how I display the lines inbetween the delimiter objects.

def look(self):
        
        NULL = None
        data = [(0,0,0,0)] ## (LINE #, STRING, Match_Obj, Del_Obj)       
        segment = re.compile('[*****]')
        lookfor = re.compile('[COMMS_MESSAGE_SENT]')
        LineNum = 0
        
        for line in self.thefilesText:
            obj = lookfor.match(line)
            deli = segment.match(line)
            data.append((LineNum,line,obj,deli))
            LineNum += 1  
        
        return data

I could be going about this the wrong way. Should I be segmenting the log file fist (between delimiters), eg, 1 segment per list element
and then using RE to search the segments (then return the entire segment when found).

any input would be great!

2
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by zyrus001
0

I'd store the blocks in a list, looking for the pattern in each line and, if i find the pattern in one line, write the whole block out.

isBlockToWrite=False
pattern="COMMS_MESSAGE_SENT"
block=[]
outfile=file("log3.log","w")
for line in file("log2.log"):
    block.append(line)
    if line[:10] == "*" * 10:
        if isBlockToWrite:
            outfile.writelines(block)
        block=[]
        isBlockToWrite=False
    if pattern in line:
        isBlockToWrite=True
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.