Remove lines that contain certain string

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

    Regex is a little quicker than the accepted answer (for my 23 MB test file) that I used. But there isn't a lot in it.

    import re
    
    bad_words = ['bad', 'naughty']
    
    regex = f"^.*(:{'|'.join(bad_words)}).*\n"
    subst = ""
    
    with open('oldfile.txt') as oldfile:
        lines = oldfile.read()
    
    result = re.sub(regex, subst, lines, re.MULTILINE) 
    
    with open('newfile.txt', 'w') as newfile:
        newfile.write(result)
    
    

提交回复
热议问题