How do I skip over multiple words in a file?

Paul Rubin no.email at nospam.invalid
Thu Nov 11 15:41:01 EST 2010


chad <cdalten at gmail.com> writes:

> Let's say that I have an article. What I want to do is read in this
> file and have the program skip over ever instance of the words "the",
> "and",  "or", and "but". What would be the general strategy for
> attacking a problem like this?

Something like (untested):

    stopwords = set (('and', 'or', 'but'))

    def goodwords():
      for line in file:
         for w in line.split():
            if w.lower() not in stopwords:
               yield w

Removing punctuation is left as an exercise.



More information about the Python-list mailing list