Remove lines that contain certain string

后端 未结 9 1352
予麋鹿
予麋鹿 2020-11-30 01:00

I\'m trying to read a text from a text file, read lines, delete lines that contain specific string (in this case \'bad\' and \'naughty\'). The code I wrote goes like this:

9条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-30 01:10

    to_skip = ("bad", "naughty")
    out_handle = open("testout", "w")
    
    with open("testin", "r") as handle:
        for line in handle:
            if set(line.split(" ")).intersection(to_skip):
                continue
            out_handle.write(line)
    out_handle.close()
    

提交回复
热议问题