Stripping a block of text from a file using python

前端 未结 1 643
南笙
南笙 2021-01-28 20:21

I have to basically strip out a block of text from file1. The file1 is huge of many thousand lines (~3GB size). The block looks as below,

line 1
line 2


        
相关标签:
1条回答
  • 2021-01-28 21:15

    Your proposal is simple to understand and implement. This also makes it easy to debug, maintain, and update. Yes, this is a good approach to use.

    start_pattern_list =   # Make this a list of all the patterns from file2
    stop_pattern = "}\n"
    out_flag == False
    for line in <input file>:
        if '<' in line:
            if any(p in line for p in start_pattern_list):
                out_flag = True     # Turn on output
        if out_flag:
            out_file.write(line)
        if stop_pattern in line:
            out_flag = False    # Turn off output
    

    This works for simple files: no nested braces, the input trigger (pattern) appears only as a pattern (not embedded within the braces).

    0 讨论(0)
提交回复
热议问题