简体   繁体   English

在python中获取所有属性XML并将其变为字典

[英]Get all Attributes XML in python and Make it into a dictionary

XML: XML:

<main>
    <item name="item1" image="a"></item>
    <item name="item2" image="b"></item>
    <item name="item3" image="c"></item>
    <item name="item4" image="d"></item>
</main>

Python: 蟒蛇:

xmldoc = minidom.parse('blah.xml')
itemlist = xmldoc.getElementsByTagName('item')
for item in itemlist :
    #####I want to make a dictionary of each item

So I would get 所以我会得到

{'name':'item1','image':'a'}
{'name':'item2','image':'b'}
{'name':'item3','image':'c'}
{'name':'item4','image':'d'}

Does anyone know how to do this? 有谁知道如何做到这一点? Is there a function? 有功能吗?

The following code will create the dictionaries (no additional libraries are needed): 以下代码将创建字典(不需要其他库):

dicts = []
for item in itemlist:
    d = {}    
    for a in item.attributes.values():
        d[a.name] = a.value
    dicts.append(d)
print dicts

I suggest to prefer the newer xml.etree.ElementTree standard module to the xml.dom.minidom . 我建议更新的xml.etree.ElementTree标准模块到xml.dom.minidom Try the following: 请尝试以下方法:

import xml.etree.ElementTree as ET

tree = ET.parse('test.xml')
for element in tree.getiterator('item'):
    print element.attrib

It prints 它打印

{'image': 'a', 'name': 'item1'}
{'image': 'b', 'name': 'item2'}
{'image': 'c', 'name': 'item3'}
{'image': 'd', 'name': 'item4'}

Here the .getiterator('item') traverses all elements of the tree and returns the elements named item . 这里.getiterator('item')遍历树的所有元素并返回名为item的元素。 The .attrib of each element is a dictionary of the element attributes -- this is exactly what you want. 每个元素的.attrib是元素属性的字典 - 这正是你想要的。

Actually, the elements behave as lists of subelements. 实际上,元素表现为子元素列表。 With the above attributes are items in the dictionary , the ElemenTree fits much better with Python than the DOM approach. 由于上述属性是字典中的项目,因此使用Python比使用DOM方法更适合ElemenTree。

Add the following code to the above sample: 将以下代码添加到上面的示例中:

print '----------------'
root = tree.getroot()
ET.dump(root)

print '----------------'
print root.tag
print root.attrib
for elem in root:
    print elem.tag, elem.attrib

It prints: 它打印:

----------------
<main>
    <item image="a" name="item1" />
    <item image="b" name="item2" />
    <item image="c" name="item3" />
    <item image="d" name="item4" />
</main>
----------------
main
{}
item {'image': 'a', 'name': 'item1'}
item {'image': 'b', 'name': 'item2'}
item {'image': 'c', 'name': 'item3'}
item {'image': 'd', 'name': 'item4'}

Using this Python recipe: 使用这个 Python配方:

from xml2obj import xml2obj

data = xml2obj(s)['item']

# data content:
>>> [{image:u'a', name:u'item1'},
>>>  {image:u'b', name:u'item2'},
>>>  {image:u'c', name:u'item3'},
>>>  {image:u'd', name:u'item4'}]

Upon actually trying this out, it errors out somewhere, but this should get you started if you don't want to use one of the other answers. 在实际尝试这个时,它会在某个地方出错,但如果您不想使用其他答案之一,这应该让您开始。

from bs4 import BeautifulSoup

xml = BeautifulSoup('''
<main>
    <item name="item1" image="a"></item>
    <item name="item2" image="b"></item>
    <item name="item3" image="c"></item>
    <item name="item4" image="d"></item>
</main>
''')

item = xml.find_all('item')

count = 0
for snippet in item:
    eval('attribute' + str(count) = {'name':item[count]['name'],
                                     'image':item[count]['image']})
    count += 1

This Python code will perform the task you want, but the output is not sorted as you indicated in your example output. 此Python代码将执行您想要的任务,但输出未按您在示例输出中指示的那样排序。 Dictionaries have access by key but are not sorted. 字典可以通过键访问,但不进行排序。

from xml.etree import ElementTree
treexml = ElementTree.parse('test.xml')
for element in treexml.getiterator():
    dict_keys={}
    if element.keys():
        for name, value in element.items():
            dict_keys[name]=value
        print dict_keys

List/dictionary comprehension version of the accepted answer : 列表/字典理解版本的接受答案

dicts = [{a.name: a.value for a in item.attributes.values()} for item in itemlist]
print dicts

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM