Python属性解析为xml:id返回无
我正在尝试使用以下代码从tei文件中提取一些信息:Python属性解析为xml:id返回无,python,python-3.x,xml-parsing,attributes,elementtree,Python,Python 3.x,Xml Parsing,Attributes,Elementtree,我正在尝试使用以下代码从tei文件中提取一些信息: tree = ET.parse(path) root = tree.getroot() body = root.find("{http://www.tei-c.org/ns/1.0}text/{http://www.tei-c.org/ns/1.0}body") for s in body.iter("{http://www.tei-c.org/ns/1.0}s"): for w in s.iter("{http://www.tei
tree = ET.parse(path)
root = tree.getroot()
body = root.find("{http://www.tei-c.org/ns/1.0}text/{http://www.tei-c.org/ns/1.0}body")
for s in body.iter("{http://www.tei-c.org/ns/1.0}s"):
for w in s.iter("{http://www.tei-c.org/ns/1.0}w"):
wordpart = w.find("{http://www.tei-c.org/ns/1.0}seg")
word = ''.join(wordpart.itertext())
type = w.get('type')
xml = w.get('xml:id')
print(type)
print(xml)
类型
的输出是正确的,它打印例如“noun”。但是对于xml:id
我只能得到None
。这是我需要解析的xml文件的摘录:
<w type="noun" xml:id="w.4940"><seg type="orth">sloterheighe</seg>...
sloterheighe。。。
要获取xml:id
属性的值,需要像这样指定名称空间URI(有关更多详细信息,请参阅):
或
另外,请注意,
type
是Python中的内置方法,因此请避免将其用作变量名。为什么xml:id=“w.4940”“
末尾有两个引号?小错误,我编辑了它,谢谢
xml = w.attrib['{http://www.w3.org/XML/1998/namespace}id']
xml = w.get('{http://www.w3.org/XML/1998/namespace}id')