2014-02-16 130 views
1

我需要一些帮助,弄清楚如何将文本文件中的单词分成列表。我可以用这样的:将多行文本文件分割成单个列表?

words = [] 
for line in open('text.txt'): 
    line.split() 
    words.append(line) 

但是,如果该文件包含多行文本,他们被分成子列表,例如

this is the first line 
this is the second line 

变为:

[['this', 'is', 'the', 'first', 'line'], ['this', 'is', 'the', 'second', 'line']] 

如何让这个他们在同一个列表?即

[['this', 'is', 'the', 'first', 'line', 'this', 'is', 'the', 'second', 'line']] 

谢谢!

编辑: 该程序将打开多个文本文件,因此每个文件中的单词都需要添加到子列表中。所以如果一个文件有多行,这些行中的所有单词应该一起存储在一个子列表中。 即每个新文件开始一个新的子列表。

回答

1

不知道为什么你要保留[[]]但是:

words = [open('text.txt').read().split()] 
3

您可以使用列表理解,这样拉平的话

[word for words in line.split() for word in words] 

名单这是一样的书写

result = [] 
for words in line.split(): 
    for word in words: 
     result.append(word) 

或者你可以使用itertools.chain.from_iterable,这样

from itertools import chain 
with open("Input.txt") as input_file: 
    print list(chain.from_iterable(line.split() for line in input_file)) 
+0

我不太清楚如何实现这个作为我的程序做一个正则表达式替换字(如果需要的话)他们是前添加到列表中,即文件中的行被分割成单词,然后检查正则表达式,然后将新的列表添加到列表中 –

2

你的代码实际上并没有做你说的那样。 line.split()只是返回行中的单词列表,您不用做任何事情;它不会以任何方式影响line,因此当您执行words.append(line)时,您只需追加原始行即单个字符串。

所以,首先,你要解决这个问题:

words = [] 
for line in open('text.txt'): 
    words.append(line.split()) 

现在,你在做什么,反复追加的话一个新的列表空列表。所以当然你会得到一个单词列表。这是因为你在混合the append and extend methods of listappend接受任何对象,并将该对象添加为列表的新元素; extend采用任何可迭代的方式,并将该迭代的每个元素添加为列表的单独新元素。

如果你解决这个问题太:

words = [] 
for line in open('text.txt'): 
    words.extend(line.split()) 

...现在你得到你想要的东西。

相关问题