从Python

时间:2017-07-25 20:25:17

标签: python xml

我尝试连接xml属性,但它只接受第一对,然后从新项目的属性开始。阅读文件时,这是有意义的。

import os, csv
from xml.etree import ElementTree


file_name = 'data.xml'
full_file = os.path.abspath(os.path.join('xml', file_name))
dom = ElementTree.parse(full_file)

with open('output.csv', 'w', newline="") as f: 
    writer = csv.writer(f)
    writer.writerow(['fruitNumber', 'categoryNumber', 'Group', 'AttributeValueName'])



for d in dom.findall('//item'):
    part = d.find('.//item-number').text
    name = d.find('.//name').text
    value = d.find('.//value').text 
    writer.writerow([part, '' , '', name + ":" + value])

这是我的xml文件:

<?xml version="1.0"?>
<all>
<items>
<item>
<item-number>449</item-number>
<attributes>
<attribute>
<name>FRUIT</name>
<value>Lemon</value>
</attribute>
<attribute>
<name>COLOR</name>
<value>Yellow</value>
</attribute>
</attributes>
</item>
<item>
<item-number>223</item-number>
<attributes>
<attribute>
<name>FRUIT</name>
<value>Orange</value>
</attribute>
<attribute>
<name>COLOR</name>
<value>Orange</value>
</attribute>
</attributes>
</item>
</items>
</all>

这是我得到的:

fruitNumber categoryNumber  Group   AttributeValueName
449                                 FRUIT:Lemon
223                                 FRUIT:Orange

以下是我想要的内容:

fruitNumber categoryNumber  Group   AttributeValueName
449                                 FRUIT:Lemon│COLOR:Yellow
223                                 FRUIT:Orange│COLOR:Orange

感谢您的帮助!!!

1 个答案:

答案 0 :(得分:1)

您只是阅读每个项目的第一个属性。您还需要在项目下搜索属性,收集它们,然后在编写行时根据需要对其进行格式化:

import os, csv
from xml.etree import ElementTree


file_name = 'data.xml'
full_file = os.path.abspath(os.path.join('xml', file_name))
dom = ElementTree.parse(full_file)

with open('output.csv', 'w', newline="") as f: 
    writer = csv.writer(f)
    writer.writerow(['fruitNumber', 'categoryNumber', 'Group', 'AttributeValueName'])

    for d in dom.findall('.//item'):
        part = d.find('.//item-number').text
        L = []
        for a in d.findall('.//attribute'):
            name = a.find('.//name').text
            value = a.find('.//value').text
            L.append('{}:{}'.format(name,value))
        writer.writerow([part, '' , '', '|'.join(L)])

<强>输出

fruitNumber,categoryNumber,Group,AttributeValueName
449,,,FRUIT:Lemon|COLOR:Yellow
223,,,FRUIT:Orange|COLOR:Orange