我已经通过美容小组解决了。在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
谢谢你们!在