Using python to write specific lines from one file to another file

前端 未结 5 1890
攒了一身酷
攒了一身酷 2020-12-05 11:41

I have ~200 short text files (50kb) that all have a similar format. I want to find a line in each of those files that contains a certain string and then write that line plus

5条回答
  •  盖世英雄少女心
    2020-12-05 12:21

    I think your problem is the tabs of the bottom file.

    You need to indent from if Lines[i] until after i=i+1 such as:

    while i < 500:
        if Lines[i] == searchquery:
            f2.write(Lines[i])
            f2.write(Lines[i+1])
            f2.write(Lines[i+2])
            i = i+1
        else:
            i = i+1
    

提交回复
热议问题