我有一个很大的文本文件,我想把它变成一个单词列表。我已经能够获得文件中每一行的列表,但我想要一个列表。这就是我所拥有的。import unicodedataimport codecsinfile = codecs.open('FILE.txt', 'r', encoding ='ascii', errors = 'ignore')outfile = codecs.open('FILE2.txt', 'w', encoding ='ascii', errors = 'ignore')for word in infile: mylist = str(word.split()) outfile.write(mylist)infile.close()outfile.close()这给了我一个看起来像的输出文件:[word, word][word, word, word, word][word, word]...[word,word]我希望得到一个看起来像这样的输出文件:[word, word, word, .... word, word, word]我知道如何连接多个列表,但这些列表会立即写入我的输出文件。正如所写,我的代码不允许我事后连接列表。更新:感谢您的帮助。我已经解决了以下问题:import unicodedataimport codecsinfile = codecs.open('FILE1.txt', 'r', encoding ='ascii', errors = 'ignore')outfile = codecs.open('FILE2.txt', 'w', encoding ='ascii', errors = 'ignore')mylist =[]for line in infile: for word in line.split(): mylist.append(word)outfile.write(str(mylist))infile.close()outfile.close()
3 回答
拉莫斯之舞
TA贡献1820条经验 获得超10个赞
在你写之前把你的清单弄平,有没有规定?
for word in infile:
mylist = str(word.split())
mylist = [j for i in mylist for j in i]
outfile.write(mylist)
添加回答
举报
0/150
提交
取消