【问题标题】:Preprocessing the data from a text file with a split method使用 split 方法预处理文本文件中的数据
【发布时间】: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


    【解决方案1】:

    split('&lt;EOS&gt;') 仅在 &lt;EOS&gt; 上拆分,如您所料。但是,for x in f: 逐行工作,因此有效地对您的文件执行隐式 split

    相反,也许可以这样做:

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:

        split() 不会将文本与'\n' '&lt;EOS&gt;' 分开,它只是针对后者。 for x in f: 有效地用换行符 (\n) 分割文件的内容。

        这里的代码与您的代码大致相同,可以更好地说明发生了什么:

        with open('./plots') as f:
            documents=[]
            for x in f:
                documents.append(x.split('<EOS>'))
        
        for i, document in enumerate(documents):
            print('documents[{}]: {!r}'.format(i, document))
        

        【讨论】: