Extract specific text lines?

前端 未结 10 1074
庸人自扰
庸人自扰 2021-02-03 15:09

I have a large several hudred thousand lines text file. I have to extract 30,000 specific lines that are all in the text file in random spots. This is the program I have to extr

10条回答
  •  抹茶落季
    2021-02-03 15:40

    Aha! So your real problem is how to test many conditions per line and if one of them is satisfied, to output that line. Easiest will be using regular expression, me thinks:

    import re
    keywords = ['S0414', 'GT213', 'AT3423', 'PR342'] # etc - you probably get those from some source
    pattern = re.compile('|'.join(keywords))
    
    for line in inf:
        if pattern.search(ln):
            outf.write(line)
    

提交回复
热议问题