如何从 Python xml etree ElementTree 中的根元素中删除属性
How to remove attribute from root element in Python xml etree ElementTree
我的文件包含以下数据:
原文:
<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <url> <changefreq>daily</changefreq> <loc>http://www.example.com</loc></url></urlset>
预计:
<?xml version="1.0" encoding="UTF-8"?><urlset> <url> <changefreq>daily</changefreq> <loc>http://www.example.com</loc></url></urlset>
我使用etree解析文件,我想从根元素中删除属性'urlset'
import xml.etree.ElementTree as ET
tree = ET.parse("/Users/hsyang/Downloads/VI-0-11-14-2016_20.xml")
root = tree.getroot()
print root.attrib
>> {}
root.attrib.pop("xmlns", None)
print root.attrib
>> {}
ET.tostring(root)
我以为我第一次打印 root.attrib 时应该得到 {xmlns:"http://www.sitemaps.org/schemas/sitemap/0.9"},但我得到的是一个空字典。有人可以帮忙吗?
欣赏!
在标准库中 xml.etree.ElementTree 没有删除属性的特殊方法,但是所有属性都存储在一个 attrib
中,这是一个 dict
并且可以从中删除任何属性attrib
作为来自 dict
的键:
import xml.etree.ElementTree as ET
tree = ET.parse(file_path)
root = tree.getroot()
print(root.attrib) # {'xyz': '123'}
root.attrib.pop("xyz", None) # None is to not raise an exception if xyz does not exist
print(root.attrib) # {}
ET.tostring(root)
'<urlset> <url> <changefreq>daily</changefreq> <loc>http://www.example.com</loc></url></urlset>'
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
看起来像一个常规属性,但它是一种特殊情况,即名称空间声明。
删除、添加或修改命名空间可能非常困难。 "Normal" 属性存储在元素的可写 attrib
属性 中。另一方面,命名空间映射不容易通过 API(在 lxml 库中,元素确实有 nsmap
属性,但它是只读的)。
我建议一个简单的文本搜索和替换操作,类似于 Modify namespaces in a given xml document with lxml 的答案。像这样:
with open("input.xml", "r") as infile, open("output.xml", "w") as outfile:
data = infile.read()
data = data.replace(' xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"', '')
outfile.write(data)
另见 How to insert namespace and prefixes into an XML string with Python?。
我的文件包含以下数据:
原文:
<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <url> <changefreq>daily</changefreq> <loc>http://www.example.com</loc></url></urlset>
预计:
<?xml version="1.0" encoding="UTF-8"?><urlset> <url> <changefreq>daily</changefreq> <loc>http://www.example.com</loc></url></urlset>
我使用etree解析文件,我想从根元素中删除属性'urlset'
import xml.etree.ElementTree as ET
tree = ET.parse("/Users/hsyang/Downloads/VI-0-11-14-2016_20.xml")
root = tree.getroot()
print root.attrib
>> {}
root.attrib.pop("xmlns", None)
print root.attrib
>> {}
ET.tostring(root)
我以为我第一次打印 root.attrib 时应该得到 {xmlns:"http://www.sitemaps.org/schemas/sitemap/0.9"},但我得到的是一个空字典。有人可以帮忙吗?
欣赏!
在标准库中 xml.etree.ElementTree 没有删除属性的特殊方法,但是所有属性都存储在一个 attrib
中,这是一个 dict
并且可以从中删除任何属性attrib
作为来自 dict
的键:
import xml.etree.ElementTree as ET
tree = ET.parse(file_path)
root = tree.getroot()
print(root.attrib) # {'xyz': '123'}
root.attrib.pop("xyz", None) # None is to not raise an exception if xyz does not exist
print(root.attrib) # {}
ET.tostring(root)
'<urlset> <url> <changefreq>daily</changefreq> <loc>http://www.example.com</loc></url></urlset>'
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
看起来像一个常规属性,但它是一种特殊情况,即名称空间声明。
删除、添加或修改命名空间可能非常困难。 "Normal" 属性存储在元素的可写 attrib
属性 中。另一方面,命名空间映射不容易通过 API(在 lxml 库中,元素确实有 nsmap
属性,但它是只读的)。
我建议一个简单的文本搜索和替换操作,类似于 Modify namespaces in a given xml document with lxml 的答案。像这样:
with open("input.xml", "r") as infile, open("output.xml", "w") as outfile:
data = infile.read()
data = data.replace(' xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"', '')
outfile.write(data)
另见 How to insert namespace and prefixes into an XML string with Python?。