Python: load words from file into a set

后端 未结 6 1941
暗喜
暗喜 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:03

    The strip() method of strings removes whitespace from both ends.

    set(line.strip() for line in open('filename.txt'))
    

提交回复
热议问题