vendredi 27 avril 2018

Usage of "If any ()" not producing desired results

I have a list of words in a csv file (in column A). I also have a file filled with 1 sentence per line (row, column A).

I need to iterate through each sentence and only keep the sentences that do contain any of the words in "list of words" csv file.

I was very sure that i had the right code, but for some reason, it just keeps writing every single sentence. Can you please have a look to see what part of my code is incorrect? I suspect that my usage of "If any()" may be incorrect.

bad_words = r'C:\Users\.spyder-py3\words.csv'

with open(r'C:\Users\.spyder-py3\newdata.csv') as oldfile, open('newfile.csv', 'w') as newfile:
    for line in oldfile:
        if any(bad_word in line for bad_word in bad_words):
            newfile.write(line)

Aucun commentaire:

Enregistrer un commentaire