如何将csv列表传递到XML findall中并返回匹配的子级

时间:2019-03-04 15:37:42

标签: python xml elementtree

在这里,我想将csv列表传递到我的python代码中,以仅检索与csv列表中匹配的Childone值。正确的方法是什么?

import xml.etree.ElementTree as ET    
import csv        
tree = ET.parse("C:/Users/thisuser/Desktop/stack.xml")
root = tree.getroot()

with open('C:/Users/thisuser/Desktop/stack.csv', 'rb') as f:
    reader = csv.reader(f)
    your_list = list(reader)

for ticket in root.findall('.//Parent'):
    Childone = ticket.find('.//Childone').text
    for Childone in your_list:

        for ticket2 in ticket.findall('.//ParentTwo'):
            Childtwo = ticket2.find('.//Childtwo').text
            print "Childone={Childone}, Childtwo={Childtwo}".format(Childone=Childone, Childtwo=Childtwo)

所需结果

ChildOne=2  ChildTwo=c
ChildOne=2  ChildTwo=d

ChildOne=3  ChildTwo=c
ChildOne=3  ChildTwo=e

当前结果

Childone=['2'], Childtwo=a
Childone=['2'], Childtwo=b
Childone=['3'], Childtwo=a
Childone=['3'], Childtwo=b
Childone=['2'], Childtwo=c
Childone=['2'], Childtwo=d
Childone=['3'], Childtwo=c
Childone=['3'], Childtwo=d
Childone=['2'], Childtwo=c
Childone=['2'], Childtwo=e
Childone=['3'], Childtwo=c
Childone=['3'], Childtwo=e

XML样本

<Parent>
    <Childone>1</Childone>
        <ParentTwo>
            <Childtwo>a</Childtwo>
        </ParentTwo>
        <ParentTwo>
            <Childtwo>b</Childtwo>
        </ParentTwo>
</Parent>

<Parent>
    <Childone>2</Childone>
        <ParentTwo>
            <Childtwo>c</Childtwo>
        </ParentTwo>
        <ParentTwo>
            <Childtwo>d</Childtwo>
        </ParentTwo>
</Parent>

<Parent>
    <Childone>3</Childone>
        <ParentTwo>
            <Childtwo>c</Childtwo>
        </ParentTwo>
        <ParentTwo>
            <Childtwo>e</Childtwo>
        </ParentTwo>
</Parent>

EXCEL样本

enter image description here

我收到此错误:

AttributeError:'NoneType'对象没有属性'text'

1 个答案:

答案 0 :(得分:1)

您可以遍历csv并仅处理具有匹配Parent元素的Childone元素(使用XPath predicate)。

此外,您已经知道Childone元素的值,因此您只需要Childtwo的值...

XML输入(test.xml)

<doc>
    <Parent>
        <Childone>1</Childone>
        <ParentTwo>
            <Childtwo>a</Childtwo>
        </ParentTwo>
        <ParentTwo>
            <Childtwo>b</Childtwo>
        </ParentTwo>
    </Parent>

    <Parent>
        <Childone>2</Childone>
        <ParentTwo>
            <Childtwo>c</Childtwo>
        </ParentTwo>
        <ParentTwo>
            <Childtwo>d</Childtwo>
        </ParentTwo>
    </Parent>

    <Parent>
        <Childone>3</Childone>
        <ParentTwo>
            <Childtwo>c</Childtwo>
        </ParentTwo>
        <ParentTwo>
            <Childtwo>e</Childtwo>
        </ParentTwo>
    </Parent>
</doc>

CSV (test.csv)

2
3

Python

import xml.etree.ElementTree as ET
import csv

tree = ET.parse("test.xml")

with open('test.csv', 'rb') as f:
    reader = csv.reader(f)
    for i, row in enumerate(reader):
        if i:
            print ""
        for childtwo in tree.findall(".//Parent[Childone='{}']/ParentTwo/Childtwo".format(row[0])):
            print "ChildOne={childone}  ChildTwo={childtwo}".format(childone=row[0], childtwo=childtwo.text)

输出

ChildOne=2  ChildTwo=c
ChildOne=2  ChildTwo=d

ChildOne=3  ChildTwo=c
ChildOne=3  ChildTwo=e