Extract specific text lines?

前端 未结 10 1107
庸人自扰
庸人自扰 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:20

    If the line begins with S0414, then you could use the .startswith method:

    if line.startswith('S0414'): small_file3.write(line)
    

    You could also strip left whitespace, if there is any:

    line.lstrip().startswith('S0414')
    

    If 'S0414' always appears after a certain point, for example, it is always at least 10 characters in and never in the last 5 characters, you could do:

    'S0414' in line[10:-5]
    

    Otherwise, you will have to search through each line, like you are.

提交回复
热议问题