![](/img/trans.png)
[英]using a Python dictionary to count the frequency of words, excluding a set of "stop words" that will be read from a second file
[英]Python: load words from file into a set
我有一个简单的文本文件,其中包含数千个单词,每个单词都在自己的行中,例如
aardvark
hello
piper
我使用以下代码将单词加载到一个集合中(我需要单词列表来测试成员资格,所以set是我选择的数据结构):
my_set = set(open('filename.txt'))
上面的代码生成一个包含以下条目的集合(每个单词后跟一个空格和换行符:
("aardvark \n", "hello \n", "piper \n")
将文件加载到集合中但删除空间和\\ n的最简单方法是什么?
谢谢
string的strip()方法从两端移除空格。
set(line.strip() for line in open('filename.txt'))
只需加载所有文件数据并将其拆分,每行将处理一个单词或每行由空格分隔多个单词,除非您的文件是GB,否则一次加载整个文件会更快
words = set(open('filename.txt').read().split())
my_set = set(map(str.strip, open('filename.txt')))
仅删除右侧空格。
set(map(str.rstrip, open('filename.txt')))
with open("filename.txt") as f:
mySet = map(str.rstrip, f)
如果你想在Python 2.5中使用它,你需要
from __future__ import with_statement
with open("filename.txt") as f:
s = set([line.rstrip('\n') for line in f])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.