0

我是 Python 的初学者。我有一个如下文本文件,其中包含数千个文档(从 id=1 到 id=10000):

<doc id=1>
    <label>1</label>
    <summary>
        I think you are right
    </summary>
    <short_text>
        I think you are right. Because I have once read the book in the same topic.
    </short_text>
</doc>

有没有方便的方法来读取文本文件并将内容存储在实例中?

class ShortText:
    def __init__(self, my_id, human_label, summary, short_text):
        self.id = my_id         
        self.human_label = human_label    
        self.summary = summary 
        self.short_text = short_text
    def __str__(self):
        '''
        For printing purposes.
        '''
        return '%d\t%s\t%s\t%s' % (self.id, self.human_label, self.summary, self.short_text)

def load_file(filename):
    #retrieve the original text 
    with codecs.open(filename, encoding='utf-8') as f:
        data = f.read()
    #how to get values from tags and put it below?
        my_id = 
        human_label = 
        summary = 
        short_text = 
        instances[my_id] = ShortText(my_id, human_label, summary, short_text)
    return instances
4

2 回答 2

1

我已经通过 BeautifulSoup 解决了这个问题。

import codecs
from bs4 import BeautifulSoup   

class ShortText:
    def __init__(self, my_id, human_label, summary, short_text):
        self.id = my_id         
        self.human_label = human_label    
        self.summary = summary 
        self.short_text = short_text
    def __str__(self):
        '''
        For printing purposes.
        '''
        return '%d\t%d\t%s\t%s' % (self.id, self.human_label, self.summary, self.short_text)

def load_file(filename):
    #retrieve the original text 
    with codecs.open(filename, encoding='utf-8') as f:
        data = f.read()
    #use beautifulsoup to get tag attributes and elements
    soup = BeautifulSoup(data)
    tags = soup.find_all('doc')
    #store in a dictionary with ShortText Instances as values
    instances = {}
    my_id = 0
    for t in tags:
        human_label = int(t.human_label.get_text())
        summary = t.summary.get_text().replace("\n", "").replace(" ", "")
        short_text = t.short_text.get_text().replace("\n", "").replace(" ", "")
        instances[my_id] = ShortText(my_id, human_label, summary, short_text)
        my_id +=1

    return instances

感谢你们!

于 2018-11-30T20:04:51.387 回答
1

如果您可以将数据视为 XML 片段,则可以尝试使用该lxml库:

测试.py:

from lxml import etree

a = etree.fromstring("<test>Hello</test>")

print a.text

结果

>>> python test.py
Hello

从文件中读取:

>>> tree = etree.parse(some_file_or_file_like_object)

https://lxml.de/tutorial.html#the-fromstring-function

https://lxml.de/tutorial.html#parser-objects

于 2018-11-30T17:43:59.593 回答