-1
我有以下XML文件:如何递归迭代有序字典?
<?xml version="1.0"?>
<catalog>
<book id="bk101">
<author a_id="a101">
<first>Gambardella </first>
<last> Matthew</last>
<email> [email protected]</email>
</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
<book id="bk112">
<author a_id='a007'>
<first>Galos</first>
<last> Mike</last>
<email>[email protected]</email>
</author>
<title>Visual Studio 7: A Comprehensive Guide</title>
<genre>Computer</genre>
<price>49.95</price>
<publish_date>2001-04-16</publish_date>
<description>Microsoft Visual Studio 7 is explored in depth,
looking at how Visual Basic, Visual C++, C#, and ASP+ are
integrated into a comprehensive development
environment.</description>
</book>
</catalog>
我已经使用xmltodict
模块,它返回一个有序词典文件转换成一个字典。此字典包含词典中的字典作为值。
from xmltodict import parse
data=open(file_name).read().replace('\n', '')
xml_dict = parse(data)
现在,我想在递归的方式直至打印xml_dict
一定深度的“键::值”格式。
任何人都可以告诉我如何以递归的方式迭代这个字典。
我已经试过代码:
def print_dict(_dict):
for k, v in _dict.items():
if hasattr(v, 'items'):
print_dict(v)
elif isinstance(v, list):
for i in v:
print_dict(dict(i))
else:
print "(%s::%s)"%(k,v)
但与此代码书标签去错过。
那么,什么将成为该解决方案.......
'elif的isinstance(V,列表):'然后下面的'print_dict(字典(I)) '。这不应该只是'印刷v(i)'? – Evert 2015-04-04 17:56:04
任何你不能使用XML解析器的原因? – salparadise 2015-04-04 19:11:19
请显示你想输出的样子。 – martineau 2015-04-06 01:26:07