Python xml.find()中的键错误
似乎命名空间映射的键不能是Python xml.find()中的键错误,python,xml,Python,Xml,似乎命名空间映射的键不能是“”。使用iterparse和startns动态获取名称空间似乎不是很好。以下是一个例子: from xml.etree.ElementTree import fromstring, iterparse import io s = '''<?xml version="1.0"?> <actors xmlns:fictional="http://characters.example.com" xmlns="http://people.ex
“”
。使用iterparse
和startns
动态获取名称空间似乎不是很好。以下是一个例子:
from xml.etree.ElementTree import fromstring, iterparse
import io
s = '''<?xml version="1.0"?>
<actors xmlns:fictional="http://characters.example.com"
xmlns="http://people.example.com">
<actor>
<name>John Cleese</name>
<fictional:character>Lancelot</fictional:character>
<fictional:character>Archie Leach</fictional:character>
</actor>
<actor>
<name>Eric Idle</name>
<fictional:character>Sir Robin</fictional:character>
<fictional:character>Gunther</fictional:character>
<fictional:character>Commander Clement</fictional:character>
</actor>
</actors>'''
# root = fromstring(s)
f = io.StringIO(s)
# get namespace dynamically
ns_map = {}
for event, elem in iterparse(f, ['start-ns']):
ns, url = elem
ns_map[ns] = url
f = io.StringIO(s)
root = parse(f)
ns_map
#{'': 'http://uniprot.org/uniprot',
# 'xsi': 'http://www.w3.org/2001/XMLSchema-instance'}
root.find(':actor', ns_map)
from xml.etree.ElementTree导入fromstring,iterparse
输入io
s='''
约翰·克莱斯
兰斯洛特
阿尔奇·利奇
埃里克·伊德尔
罗宾爵士
冈瑟
克莱门特指挥官
'''
#root=fromstring(s)
f=io.StringIO(s)
#动态获取命名空间
ns_-map={}
对于事件,iterparse中的元素(f,['start-ns']):
ns,url=elem
ns_map[ns]=url
f=io.StringIO(s)
root=parse(f)
ns_地图
#{'': 'http://uniprot.org/uniprot',
#'xsi':'http://www.w3.org/2001/XMLSchema-instance'}
root.find(':actor',ns\u映射)
它将给出以下错误:
262 try:
--> 263 selector = _cache[cache_key]
264 except KeyError:
KeyError: (':actor', (('', 'http://people.example.com'), ('fictional', 'http://characters.example.com')))
During handling of the above exception, another exception occurred:
KeyError Traceback (most recent call last)
<ipython-input-32-b6e4ecf5f7e9> in <module>()
----> 1 x.find(':actor', ns_map)
/Users/zech/anaconda/envs/py35/lib/python3.5/xml/etree/ElementTree.py in find(self, path, namespaces)
647 FutureWarning, stacklevel=2
648 )
--> 649 return self._root.find(path, namespaces)
650
651 def findtext(self, path, default=None, namespaces=None):
/Users/zech/anaconda/envs/py35/lib/python3.5/xml/etree/ElementPath.py in find(elem, path, namespaces)
296
297 def find(elem, path, namespaces=None):
--> 298 return next(iterfind(elem, path, namespaces), None)
299
300 ##
/Users/zech/anaconda/envs/py35/lib/python3.5/xml/etree/ElementPath.py in iterfind(elem, path, namespaces)
275 while 1:
276 try:
--> 277 selector.append(ops[token[0]](next, token))
278 except StopIteration:
279 raise SyntaxError("invalid path")
KeyError: ':'
262尝试:
-->263选择器=_缓存[缓存_键]
264除KeyError外:
KeyError:(':actor',(('','http://people.example.com","虚构的",http://characters.example.com')))
在处理上述异常期间,发生了另一个异常:
KeyError回溯(最近一次呼叫最后一次)
在()
---->1 x.find(':actor',ns_映射)
/查找中的Users/zech/anaconda/envs/py35/lib/python3.5/xml/etree/ElementTree.py(self、path、名称空间)
647未来警告,堆栈级别=2
648 )
-->649返回self.\u root.find(路径、名称空间)
650
651 def findtext(self、path、default=None、namespace=None):
/查找中的Users/zech/anaconda/envs/py35/lib/python3.5/xml/etree/ElementPath.py(元素、路径、名称空间)
296
297 def find(元素、路径、名称空间=无):
-->298返回下一个(iterfind(元素、路径、名称空间),无)
299
300 ##
/iterfind(元素、路径、名称空间)中的Users/zech/anaconda/envs/py35/lib/python3.5/xml/etree/ElementPath.py
275而1:
276试试:
-->277选择器。追加(ops[token[0]](下一步,token))
278除停止迭代外:
279 raise SYNTEXERROR(“无效路径”)
KeyError:':'
这是虫子吗?最好的解决方案是什么?您必须为空名称空间指定一个非空的“别名”:
ns_map["empty"] = ns_map[""]
print(root.find('empty:actor', ns_map))
如果这适用于您的情况,您也可以这样做。您必须为空命名空间指定一个非空的“别名”:
ns_map["empty"] = ns_map[""]
print(root.find('empty:actor', ns_map))
如果这适用于您的情况,您也可以这样做。
s
应该是unicode
而不是str
…这似乎不相关。我想这取决于您的情况。你确定它没有阻塞在:actor
…上吗?s
应该是unicode
而不是str
…这似乎不相关。我想这要看情况而定。你确定它没有被:actor
…卡住吗?是的。这就是我克服它所做的。似乎xml
模块没有理由不允许'
作为键。为什么它没有被修复…是的。这就是我克服它所做的。似乎xml
模块没有理由不允许'
作为键。为什么它不是固定的。。。