问题描述
我是python的新手,请一视同仁。
当我尝试将XML内容转换为字典列表时,我得到了输出,但没有达到预期的效果,并且尝试了很多尝试。
I'm very new to python and please treat me as same.When i tried to convert the XML content into List of Dictionaries I'm getting output but not as expected and tried a lot playing around.
XML内容:
<project>
<panelists>
<panelist panelist_login="pradeep">
<login/>
<firstname/>
<lastname/>
<gender/>
<age>0</age>
</panelist>
<panelist panelist_login="kumar">
<login>kumar</login>
<firstname>kumar</firstname>
<lastname>Pradeep</lastname>
<gender/>
<age>24</age>
</panelist>
</panelists>
</project>
我使用的代码:
import xml.etree.ElementTree as ET
tree = ET.parse(xml_file.xml) # import xml from
root = tree.getroot()
Panelist_list = []
for item in root.findall('./panelists/panelist'): # find all projects node
Panelist = {} # dictionary to store content of each projects
panelist_login = {}
panelist_login = item.attrib
Panelist_list.append(panelist_login)
for child in item:
Panelist[child.tag] = child.text
Panelist_list.append(Panelist)
print(Panelist_list)
输出:
[{
'panelist_login': 'pradeep'
}, {
'login': None,
'firstname': None,
'lastname': None,
'gender': None,
'age': '0'
}, {
'panelist_login': 'kumar'
}, {
'login': 'kumar',
'firstname': 'kumar',
'lastname': 'Pradeep',
'gender': None,
'age': '24'
}]
我期待以下输出
[{
'panelist_login': 'pradeep',
'login': None,
'firstname': None,
'lastname': None,
'gender': None,
'age': '0'
}, {
'panelist_login': 'kumar'
'login': 'kumar',
'firstname': 'kumar',
'lastname': 'Pradeep',
'gender': None,
'age': '24'
}]
我在xml树上引用了很多堆栈溢出问题,但仍然没有帮助我。
I have refereed so many stack overflow questions on xml tree but still didn't helped me.
任何帮助/建议都值得赞赏。
any help/suggestion is appreciated.
推荐答案
您的代码将带有标签属性的字典 panelist_login
附加到列表中,在此行: Panelist_list.append(panelist_login)
单独与 Panelist
字典相符。因此,对于每个< panelist>
标签,该代码都会附加2个命令:一个标签属性命令和一个子标签命令。在循环中,您有2个 append()
调用,这意味着每次循环中列表中都有2个项目。
Your code is appending the dict panelist_login
with the tag attributes to the list, in this line: Panelist_list.append(panelist_login)
separately from the Panelist
dict. So for every <panelist>
tag the code appends 2 dicts: one dict of tag attributes and one dict of subtags. Inside the loop you have 2 append()
calls, which means 2 items in the list for each time through the loop.
但是您实际上希望为每个< panelist>
标签使用一个字典,并且您希望tag属性在 Panelist
字典中出现在内部中,就好像它也是一个子标签一样。
But you actually want a single dict for each <panelist>
tag, and you want the tag attribute to appear inside the Panelist
dict as if it were a subtag also.
因此,只有一个字典,并使用标签属性更新 Panelist
词典,而不是将标签属性保留在
So have a single dict, and update the Panelist
dict with the tag attributes instead of keeping the tag attributes in a separate dict.
for item in root.findall('./panelists/panelist'): # find all projects node
Panelist = {} # dictionary to store content of each projects
panelist_login = item.attrib
Panelist.update(panelist_login) # make panelist_login the first key of the dict
for child in item:
Panelist[child.tag] = child.text
Panelist_list.append(Panelist)
print(Panelist_list)
我得到以下输出,我想这就是您的初衷:
I get this output, which I think is what you had in mind:
[
{'panelist_login': 'pradeep',
'login': None,
'firstname': None,
'lastname': None,
'gender': None,
'age': '0'},
{'panelist_login': 'kumar',
'login': 'kumar',
'firstname': 'kumar',
'lastname': 'Pradeep',
'gender': None,
'age': '24'}
]
这篇关于将XML转换为python中的词典列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!