亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在Python中讀取帶有標簽的txt文件?

如何在Python中讀取帶有標簽的txt文件?

慕森王 2021-09-02 16:19:57
我是 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
查看完整描述

2 回答

  • 2 回答
  • 0 關注
  • 300 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號