Remove lines that contain certain string

后端 未结 9 1380
予麋鹿
予麋鹿 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:12

    You could simply not include the line into the new file instead of doing replace.

    for line in infile :
         if 'bad' not in line and 'naughty' not in line:
                newopen.write(line)
    

提交回复
热议问题