我的文件包含以下数据:
原来的:
<?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 "} 但我得到了一个空字典。有人可以帮忙吗?
欣赏它!