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 the words into a set (I need the list of words to test membership, so set is the data structure I chose):
my_set = set(open('filename.txt'))
The above code produces a set with the following entries (each word is followed by a space and new-line character:
("aardvark \n", "hello \n", "piper \n")
What's the simplest way to load the file into a set but get rid of the space and \n?
Thanks
If you want to use this in Python 2.5, you need
The strip() method of strings removes whitespace from both ends.
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
To remove only the right hand spaces.