Remove lines that contain certain string

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

    I have used this to remove unwanted words from text files:

    bad_words = ['abc', 'def', 'ghi', 'jkl']
    
    with open('List of words.txt') as badfile, open('Clean list of words.txt', 'w') as cleanfile:
        for line in badfile:
            clean = True
            for word in bad_words:
                if word in line:
                    clean = False
            if clean == True:
                cleanfile.write(line)
    

    Or to do the same for all files in a directory:

    import os
    
    bad_words = ['abc', 'def', 'ghi', 'jkl']
    
    for root, dirs, files in os.walk(".", topdown = True):
        for file in files:
            if '.txt' in file:
                with open(file) as filename, open('clean '+file, 'w') as cleanfile:
                    for line in filename:
                        clean = True
                        for word in bad_words:
                            if word in line:
                                clean = False
                        if clean == True:
                            cleanfile.write(line)
    

    I'm sure there must be a more elegant way to do it, but this did what I wanted it to.

提交回复
热议问题