简体   繁体   English

没有使用 python 从 XML 中的元素标签获取所有属性

[英]Not getting all the attributes from element tags in XML using python

I have a XML file test.xml and code I have is reading the only 1st attribute, not all the other attributes我有一个 XML 文件 test.xml 并且我拥有的代码是读取唯一的第一个属性,而不是所有其他属性

test1.xml

<?xml version="1.0"?>
<?xml-stylesheet href="catalog.xsl" type="text/xsl"?>
<!DOCTYPE catalog SYSTEM "catalog.dtd">
<catalog>
   <product description="Cardigan Sweater" product_image="cardigan.jpg">
      <catalog_item gender="Men's">
         <item_number sep = "help" dep = "paraug" note = "zempu">QWZ5671</item_number>
         <line cap = "delp" des = "laraug" fote = "cat">QWZ5671</line>
         <cool_number>QWZ5671</cool_number>
         <price>39.5</price>
      </catalog_item>
   </product>
</catalog>

code:代码:

from lxml import etree
from collections import defaultdict

root_1 = etree.parse('test1.xml').getroot()
d1= []
for node in root_1.findall('.//catalog_item'):
    item = defaultdict(list)
    for x in node.iter():
        if x.attrib:
            item[x.attrib.keys()[0]].append(x.attrib.values()[0])
        if x.text.strip():
            item[x.tag].append(x.text.strip())
    d1.append(dict(item))

d1 = sorted(d1, key = lambda x: x['item_number'])
print(d1)

Current output: values of 1st arributes from each element ie sep from <item_number> and cap from <line> are being fetched into the dictionary, NOT dep and note from <item_number> and des and fote from <line>当前 output:每个元素的第一个 arributes 的值,即来自<item_number>sep和来自<line>cap被提取到字典中,而不是来自<item_number>depnote以及来自 < <line> > 的desfote

[{'gender': ["Men's"], 'sep': ['help'], 'item_number': ['QWZ5671'], 'cap': ['delp'], 'line': ['QWZ5671'], 'cool_number': ['QWZ5671'], 'price': ['39.5']}]

Expected output: to fetch all other attributes also预期 output:也获取所有其他属性

[{'gender': ["Men's"], 'sep': ['help'], 'dep': ['paraug'], 'note': ['zempu'],'item_number': ['QWZ5671'], 'cap': ['delp'], 'des': ['laraug'], 'fote': ['cat'], 'line': ['QWZ5671'], 'cool_number': ['QWZ5671'], 'price': ['39.5']}]

try this,尝试这个,

for node in root_1.findall('.//catalog_item'):
    item = defaultdict(list)
    for x in node.iter():
        # iterate over the items
        for k, v in x.attrib.items():
            item[k].append(v)

        if x.text.strip():
            item[x.tag].append(x.text.strip())

    d1.append(dict(item))

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

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