mardi 6 septembre 2016

Python 2.7 if not any syntax?

I just read a brilliant reply from Sloth at Remove lines that contain certain string question whilst searching for a way to filter out garbage lines in a txt / csv file. The gist is "take x y z words/strings/whatever from input file, then filter through each line writing only the unfiltered lines."

The code he posted was:

bad_words = ['bad', 'naughty']

with open('oldfile.txt') as oldfile, open('newfile.txt', 'w') as newfile:
for line in oldfile:
    if not any(bad_word in line for bad_word in bad_words):
        newfile.write(line)

My question is: Would someone explain the line if not any(bad_word in line for bad_word in bad_words): ?

I tried just putting in if not any(bad_word in line): but it gave me an error.

I am trying to understand why. A cursory search at python docs webpage didn't help me (I'm new to Python/programming and might not be too bright to boot :-) ).

Any references for me to read is appreciated.

Thanks!

Aucun commentaire:

Enregistrer un commentaire