Skip to content

Instantly share code, notes, and snippets.

@cizixs
Last active December 20, 2015 19:28
Show Gist options
  • Save cizixs/6182945 to your computer and use it in GitHub Desktop.
Save cizixs/6182945 to your computer and use it in GitHub Desktop.
create a iterator to process every word in a file. from Python Cookbook!
def words_of_file(thefilepath, line_to_words=str.split)
the_file = open(thefilepath)
for line in the_file:
for word in line_to_words(line):
yield word
the_file.close()
for world in worlds_of_file(thefilepath):
dosomethingwith(word)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment