How to grep lines between two patterns in a big file with python

前端 未结 5 1981
青春惊慌失措
青春惊慌失措 2021-01-03 12:45

I have a very big file, like this:

[PATTERN1]
line1
line2
line3 
...
...
[END PATTERN]
[PATTERN2]
line1 
line2
...
...
[END PATTERN]

I need to extract

5条回答
  •  感情败类
    2021-01-03 12:54

    I am kind of a new python programmer so I only barely understand your solution, but it seems like there is a lot of unnecessary iteration going on. First you read in the file, then you iterate through the file once for each item in name_list. Also, I don't know if you plan to iterate over newfile later to actually write it to a file.

    Here is how I would do it, though I realize it isn't the most pythonic looking solution. You'll only iterate over the file once though. (As a disclaimer, I didn't test this out.)

    patterns = {'startPattern1':"endPattern1", 'startPattern2':"endPattern2", 'startPattern3':"endPattern3"}
    
    fileIn = open(filenameIn, 'r')
    fileOut = open(filenameOut, 'w')
    targetEndPattern = None
    
    for line in fileIn:
       if targetEndPattern is not None:
           if line == targetEndPattern:
               targetEndPattern = None
           else:
               fileOut.write(line + "\n")
       elif line in patterns:
           targetEndPattern = patterns[line]
    

    EDIT: If you are expecting the patterns in a certain order, then this solution would have to be revised. I wrote this under the assumption that the order of the patterns doesn't matter but each start pattern matches a specific end pattern.

提交回复
热议问题