I have a simple text file with several thousands of words, each in its own line, e.g.
aardvark
hello
piper
I use the following code to load
Just load all file data and split it, it will take care of one word per line or multiple words per line separated by spaces, also it will be faster to load whole file at once unless your file is in GBs
words = set(open('filename.txt').read().split())
The strip() method of strings removes whitespace from both ends.
set(line.strip() for line in open('filename.txt'))
my_set = set(map(str.strip, open('filename.txt')))
with open("filename.txt") as f:
s = set([line.rstrip('\n') for line in f])
with open("filename.txt") as f:
mySet = map(str.rstrip, f)
If you want to use this in Python 2.5, you need
from __future__ import with_statement
To remove only the right hand spaces.
set(map(str.rstrip, open('filename.txt')))