Python: load words from file into a set

后端 未结 6 1976
暗喜
暗喜 2020-12-29 20:14

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

6条回答
  •  醉话见心
    2020-12-29 21:17

    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
    

提交回复
热议问题