Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何搜索具有匹配结束标记的xml标记?_Python_Xml_Regex - Fatal编程技术网

Python 如何搜索具有匹配结束标记的xml标记?

Python 如何搜索具有匹配结束标记的xml标记?,python,xml,regex,Python,Xml,Regex,我想知道python中搜索和删除XML标记、其中的内容(不管是什么内容)以及结束标记的最佳方法是什么?XML也是格式良好的。您可以使用XPath标识元素,然后使用以下方法: 将xml.etree.ElementTree作为ET导入 数据=“”\ ... ... ... ''' doc=ET.fromstring(数据) e=doc.find('node2/[@a1=“x2”]”) 删除文件(e) 打印(ET.tostring(文档)) # # ... # ... #

我想知道python中搜索和删除XML标记、其中的内容(不管是什么内容)以及结束标记的最佳方法是什么?XML也是格式良好的。

您可以使用XPath标识元素,然后使用以下方法:

将xml.etree.ElementTree作为ET导入
数据=“”\
... 
... 
... 
'''
doc=ET.fromstring(数据)
e=doc.find('node2/[@a1=“x2”]”)
删除文件(e)
打印(ET.tostring(文档))
# 
#    ... 
#    ... 
# 
import xml.etree.ElementTree as ET
data = '''\
<node1>
  <node2 a1="x1"> ... </node2>
  <node2 a1="x2"> ... </node2>
  <node2 a1="x1"> ... </node2>
</node1>
'''
doc = ET.fromstring(data)
e = doc.find('node2/[@a1="x2"]')
doc.remove(e)
print(ET.tostring(doc))
# <node1>
#   <node2 a1="x1"> ... </node2>
#   <node2 a1="x1"> ... </node2>
# </node1>