我需要一些帮助,弄清楚如何将文本文件中的单词分成列表。我可以用这样的:将多行文本文件分割成单个列表?
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']]
谢谢!
编辑: 该程序将打开多个文本文件,因此每个文件中的单词都需要添加到子列表中。所以如果一个文件有多行,这些行中的所有单词应该一起存储在一个子列表中。 即每个新文件开始一个新的子列表。
我不太清楚如何实现这个作为我的程序做一个正则表达式替换字(如果需要的话)他们是前添加到列表中,即文件中的行被分割成单词,然后检查正则表达式,然后将新的列表添加到列表中 –