【问题标题】:Parsing an xml file with an emphasis tag in it in python在python中解析带有强调标签的xml文件
【发布时间】:2020-07-23 14:08:58
【问题描述】:

我目前正在编写一个可以提取 xml 文件中所有文本的 python 脚本。我正在使用元素树库来解释数据,但我遇到了这个问题,但是当数据的结构像这样时......

<Segment StartTime="639.752" EndTime="642.270" Participant="fe016">
  But I bet it's a good <Pause/> superset of it.
</Segment>

当我尝试读出文本时,我在暂停标记之前得到了句段的前半部分(“好吧。所以我们有什么”)。

我想弄清楚是否有办法忽略数据段中的标签并打印出所有文本。

【问题讨论】:

标签: python xml elementtree


【解决方案1】:

另一种解决方案。

from simplified_scrapy import SimplifiedDoc,req,utils
html = '''<Segment StartTime="639.752" EndTime="642.270" Participant="fe016">
  But I bet it's a good <Pause/> superset of it.
</Segment>'''
doc = SimplifiedDoc(html)
print(doc.Segment)
print(doc.Segment.text)

结果:

{'StartTime': '639.752', 'EndTime': '642.270', 'Participant': 'fe016', 'tag': 'Segment', 'html': "\n  But I bet it's a good <Pause /> superset of it.\n"}
But I bet it's a good superset of it.

这里有更多示例。 https://github.com/yiyedata/simplified-scrapy-demo/blob/master/doc_examples

【讨论】:

    【解决方案2】:
    xml = '''<Segment StartTime="639.752" EndTime="642.270" Participant="fe016">
      But I bet it's a good <Pause/> superset of it.
    </Segment>'''
    
    # solution using ETree
    from xml.etree import ElementTree as ET
    
    root = ET.fromstring(xml)
    pause = root.find('./Pause')
    print(root.text + pause.tail)
    

    【讨论】:

      猜你喜欢
      • 2020-11-30
      • 1970-01-01
      • 1970-01-01
      • 2019-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多