【发布时间】:2019-09-06 02:27:00
【问题描述】:
这是我的代码:
files = open('clean.txt').readlines()
print files
finallist = []
for items in files:
new = items.split()
new.append(finallist)
由于文本文件太大,这里以“打印文件”为例:
files = ['chemistry leads outstanding another story \n', 'rhapsodic moments blow narrative prevent bohemian rhapsody']
我真的需要将由 '\n' 分隔的每一行拆分为单词并放入列表列表中,就像下面的格式一样:
outcome = [['chemistry','leads','outstanding', 'another', 'story'],['rhapsodic','moments','blow', 'narrative', 'prevent', 'bohemian', 'rhapsody']]
我已经尝试过类似于给出的第一个代码的方法,它返回一个空列表。请帮忙!提前致谢。
【问题讨论】:
-
finallist.append(new)?也许在发布之前至少进行校对会很好...... -
嘿@Julien,对不起,这不是校对。我真的犯了那个错误。愚蠢的错误。非常感谢!我要关闭它。
标签: python python-2.7 list split append