【发布时间】:2018-12-18 03:17:49
【问题描述】:
我在下面写了一个文本示例。我想要的是将此文本附加到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'.
【问题讨论】:
标签: python list split python-2.x