首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Minidom提取子节点

Minidom提取子节点
EN

Stack Overflow用户
提问于 2017-02-10 10:16:45
回答 1查看 74关注 0票数 0

我有一些XML:

代码语言:javascript
复制
<sentence id="1086415:2">
 <text>$6 and there is much tasty food, all of it fresh and continually refilled.</text>
  <Opinions>
   <Opinion to="31" from="27" polarity="positive" category="FOOD#STYLE_OPTIONS" target="food"/>
   <Opinion to="31" from="27" polarity="positive" category="FOOD#QUALITY" target="food"/>
   <Opinion to="31" from="27" polarity="positive" category="FOOD#PRICES" target="food"/>
  </Opinions>
</sentence>
<sentence id="1086415:3">
 <text>I am not a vegetarian but, almost all the dishes were great.</text>
  <Opinions>
   <Opinion to="48" from="42" polarity="positive" category="FOOD#QUALITY" target="dishes"/>
  </Opinions>

我试图提取意见标签中的所有内容,以便将其与元组中的文本相结合。我想知道我怎么能用迷你空间做这件事?当前意见返回“\n”。

代码语言:javascript
复制
from xml.dom import minidom
xmldoc = minidom.parse("ABSA16_Restaurants_Train_SB1_v2.xml")
sentences = xmldoc.getElementsByTagName("sentence")
for sentence in sentences:
   text = sentence.getElementsByTagName("text")[0].firstChild.data
   opinion = sentence.getElementsByTagName("Opinions")[0].firstChild.data

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-10 15:28:19

你确定你需要minidom

从医生那里:

还不熟悉DOM的用户应该考虑使用xml.etree.ElementTree模块来进行XML处理。

没有充分的理由不要浪费时间使用标准的python,它在手册中有足够的例子来解决您的任务。如果遇到麻烦的话,可以随时征求意见。

更重要的是,如果您需要经常使用XML,我建议第三方lxml,它是一个更强大的工具,包括一些电池。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42156805

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档