如何从python中的XML文件中读取注释文本 [英] How to read commented text from XML file in python

查看:175
本文介绍了如何从python中的XML文件中读取注释文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够使用'import xml.etree.ElementTree as et'来读取xml文件.但是我的问题是要读取数据文件中给出的注释文本,该如何读取:例如,在下面的xml中,我要阅读 BaseVehicle 1997年凯迪拉克Catera

I am able to read the xml file in a using 'import xml.etree.ElementTree as et'. But my problem is to read the commented text given in the data file, how to read this: For example in the below xml, I want to read BaseVehicle is 1997 Cadillac Catera

<App action="A" id="1">
    <BaseVehicle id="8559"/>
    <!--  1997 Cadillac Catera  -->
    <Qty>1</Qty>
    <PartType id="4472"/>
    <!--  Electrical/Headlight/Switch  -->
    <Part>SW1406</Part>
</App>

推荐答案

ElementTree的标准行为是忽略注释.但是,可以使用自定义解析器对象保留注释.在 Python 3.8 (其中 xml可以将.etree.ElementTree.TreeBuilder 目标配置为处理注释事件,以便将其包括在生成的树中.

The standard behaviour of ElementTree is to ignore comments. However, comments can be preserved by using a custom parser object. This has become easier in Python 3.8, where the xml.etree.ElementTree.TreeBuilder target can be configured to process comment events in order to include them in the generated tree.

from xml.etree import ElementTree as ET

parser = ET.XMLParser(target=ET.TreeBuilder(insert_comments=True)) # Python 3.8
tree = ET.parse("app.xml", parser)

# Get the comment nodes
for node in tree.iter():
    if "function Comment" in str(node.tag): 
        print(node.text)

输出:

  1997 Cadillac Catera  
  Electrical/Headlight/Switch  

使用旧版本的Python,需要更多代码.请参见忠实地将注释保留为已解析的XML .

With older versions of Python, some more code is needed. See Faithfully Preserve Comments in Parsed XML.

这篇关于如何从python中的XML文件中读取注释文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆