将xml文件转换为字典

时间:2015-11-21 10:38:29

标签: python xml dictionary

我得到了以下xml(部分文件):

<BikoretNo>8</BikoretNo>
<DllVerNo>8.0.1.0</DllVerNo>
-<Item>
   <ItemCode>11210000094</ItemCode>
   <ItemName>tobasco 60 m"m</ItemName>
   <ManufactureCountry>US</ManufactureCountry>
   <Quantity>60.00</Quantity>
   <ItemPrice>12.80</ItemPrice>
</Item>
-<Item>
   <ItemCode>13495113506</ItemCode>
   <ItemName>klik 75</ItemName>
   <ManufactureCountry>IL</ManufactureCountry>
   <Quantity>75.00</Quantity>
   <ItemPrice>5.00</ItemPrice>
</Item>

我需要将以下xml文件转换为以下字典:

{
  11210000094 :{
               ItemCode:11210000094,
               ItemName:klik 75,ManufactureCountry:IL,
               ...
               },
 13495113506: {
               ItemCode:13495113506,
               ...
              }
 }

1 个答案:

答案 0 :(得分:2)

对于此示例xml数据,我保存到名为filename.xml的文件:

<?xml version="1.0" encoding="utf-8"?> 
<body> 
<BikoretNo>8</BikoretNo>
<DllVerNo>8.0.1.0</DllVerNo>
<Item> 
   <ItemCode>11210000094</ItemCode>
   <ItemName>tobasco 60 m"m</ItemName>
   <ManufactureCountry>US</ManufactureCountry>
   <Quantity>60.00</Quantity>
   <ItemPrice>12.80</ItemPrice>
</Item>
<Item> 
   <ItemCode>13495113506</ItemCode>
   <ItemName>klik 75</ItemName>
   <ManufactureCountry>IL</ManufactureCountry>
   <Quantity>75.00</Quantity>
   <ItemPrice>5.00</ItemPrice>
</Item>
</body>

代码如下:

import xml.etree.ElementTree as ET

root = ET.parse('filename.xml').getroot()
data_set = {}

for node in root:
    if node.tag == 'Item':
        item = {child.tag: child.text for child in node}
        item_code = item['ItemCode']
        data_set[item_code] = item
print data_set

输出看起来像(为了便于阅读,我将其格式化):

{
    '11210000094': {
        'ItemPrice': '12.80',
        'ItemCode': '11210000094',
        'ItemName': 'tobasco 60 m"m',
        'ManufactureCountry': 'US',
        'Quantity': '60.00'
     },
    '13495113506': {
        'ItemPrice': '5.00', 
        'ItemCode': '13495113506', 
        'ItemName': 'klik 75', 
        'ManufactureCountry': 'IL', 
        'Quantity': '75.00'
    }
}