猿问

在python中解析带有强调标签的xml文件

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

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

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

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


慕娘9325324
浏览 137回答 2
2回答

守着星空守着你

另一种解决方案。from simplified_scrapy import SimplifiedDoc,req,utilshtml = '''<Segment StartTime="639.752" EndTime="642.270" Participant="fe016">&nbsp; 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&nbsp; 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

萧十郎

xml = '''<Segment StartTime="639.752" EndTime="642.270" Participant="fe016">&nbsp; But I bet it's a good <Pause/> superset of it.</Segment>'''# solution using ETreefrom xml.etree import ElementTree as ETroot = ET.fromstring(xml)pause = root.find('./Pause')print(root.text + pause.tail)
随时随地看视频慕课网APP

相关分类

Python
我要回答