我有一些XML:
<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”。
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谢谢。
发布于 2017-02-10 15:28:19
你确定你需要minidom吗
从医生那里:
还不熟悉DOM的用户应该考虑使用xml.etree.ElementTree模块来进行XML处理。
没有充分的理由不要浪费时间使用标准的python,它在手册中有足够的例子来解决您的任务。如果遇到麻烦的话,可以随时征求意见。
更重要的是,如果您需要经常使用XML,我建议第三方lxml,它是一个更强大的工具,包括一些电池。
https://stackoverflow.com/questions/42156805
复制相似问题