首页 > 解决方案 > 使用 split 方法预处理文本文件中的数据

问题描述

我在下面写了一个文本样本。我想要的是将此文本附加到python中的列表数据结构中。我首先将此文本'<EOS>'用作分隔符。然后将 split 方法的结果的每个元素附加到列表数据类型中。

但我面临的是该split方法将文本'\n'与其'<EOS>'分隔符分隔开来。因此,现在将一行添加到列表数据类型,但不是完整部分。

请查看下面示例文本后面的代码,让我知道我做错了什么。

Old Major, the old boar on the Manor Farm, summons the animals on the farm together for a meeting, during which he refers to humans as "enemies" and teaches the animals a revolutionary song called "Beasts of England".
When Major dies, two young pigs, Snowball and Napoleon, assume command and consider it a duty to prepare for the Rebellion.<EOS>
Alex is a 15-year-old living in near-future dystopian England who leads his gang on a night of opportunistic, random "ultra-violence".
Alex's friends ("droogs" in the novel's Anglo-Russian slang, 'Nadsat') are Dim, a slow-witted bruiser who is the gang's muscle; Georgie, an ambitious second-in-command; and Pete, who mostly plays along as the droogs indulge their taste for ultra-violence.
Characterised as a sociopath and a hardened juvenile delinquent, Alex also displays intelligence, quick wit, and a predilection for classical music; he is particularly fond of Beethoven, referred to as "Lovely Ludwig Van".`

将文档读入列表类型的 Python 代码:

f=open('./plots')
documents=[]
for x in f:
    documents.append(x.split('<EOS>'))
print documents[0]

#documents[0] must start from 'Old Major' and stops at 'Rebellion'.

标签: pythonlistsplitpython-2.x

解决方案


循环 f 会导致文件内容被换行符分割。改用这个:

f=open('./plots')
documents=f.read().split('<EOS>')
print documents[0]

推荐阅读