Don't forget to also checkout my second blog containing articles to all other related ICT topics!!

Tuesday, May 22, 2012

Creating set of unique words from a text file

Quick way of creating set of words from file. Content of file c:/tmp/story.txt
This is a story about a sailor and a whale called Moby Dick

words =  set((str.upper(file('c:/tmp/story.txt').read())).split())
print words

set(['A', 'AND', 'STORY', 'THIS', 'IS', 'DICK', 'ABOUT', 'SAILOR', 'MOBY', 'WHALE', 'CALLED'])

No comments:

Post a Comment