在使用ElementTree进行解析之前,如何检查XML中标记的存在?

时间:2019-01-17 05:32:03

标签: python xml csv parsing elementtree

我是python的新手,目前正在尝试解析XML并将其转换为CSV。如果父级和子级标签存在,我的代码将起作用,但是我收到以下错误消息:

电话=元素[3] [0]。文本 IndexError:子索引超出范围

当标签存在于第一个属性而不是第二个属性中时。

我试图放入一个if语句,但效果不佳。这就是xml和我的原始代码的样子。如果有人能指出正确的方向,我将不胜感激!

XML文件

        <Member>
        <Person>
          <FirstName>JOHN</FirstName>
          <LastName>DOE</LastName>
          <Address>
            <Address1>1234 TEST DR</Address1>
            <Address2></Address2>
            <City>SIMCITY</City>
            <State>TD</State>
            <ZipCode>12345    </ZipCode>
          </Address>
          <Phone>
            <AreaCode>212</AreaCode>
            <PhoneNumber>2223333</PhoneNumber>
          </Phone>
        </Person>
        <Person>
          <FirstName>JANE</FirstName>
          <LastName>DOE</LastName>
          <Address>
            <Address1>1234 DEE ST</Address1>
            <Address2></Address2>
            <City>LCITY</City>
            <State>TD</State>
            <ZipCode>12345    </ZipCode>
          </Address>
        </Person>
        </Member>

我的代码:

        import  csv
        import xml.etree.ElementTree as ET

        tree = ET.parse("Stack.xml")
        root = tree.getroot()

        xml_data_to_csv =open('Out.csv','w')

        Csv_writer=csv.writer(xml_data_to_csv)
        list_head=[]

        count=0
        for element in root.findall('Person'):
            person = []
            address_list = []
            phone_list = []

            #get head node
            if count == 0:
               FirstName = element.find('FirstName').tag
               list_head.append(FirstName)

               LastName = element.find('LastName').tag
               list_head.append(LastName)

               Address = element[2].tag
               list_head.append(Address)

               Phone = element[3].tag
               list_head.append(Phone)

               Csv_writer.writerow(list_head)
               count = count +1

            #get child node
            FirstName = element.find('FirstName').text
            person.append(FirstName)

            LastName = element.find('LastName').text
            person.append(LastName)

            Address = element[2][0].text
            address_list.append(Address)
            Address2 = element[2][1].text
            address_list.append(Address2)
            City = element[2][2].text
            address_list.append(City)
            State = element[2][3].text
            address_list.append(State)
            ZipCode = element[2][4].text
            address_list.append(ZipCode)
            person.append(address_list)

            Phone = element[3][0].text
            phone_list.append(Phone)
            AreaCode = element[3][1].text
            phone_list.append(AreaCode)
            person.append(phone_list)

            #Write List_nodes to csv
            Csv_writer.writerow(person)
        xml_data_to_csv.close()

1 个答案:

答案 0 :(得分:0)

尝试使用xpath查找所需的标签,例如,您可以替换以下代码:

Phone = element[3][0].text
phone_list.append(Phone)
AreaCode = element[3][1].text
phone_list.append(AreaCode)
person.append(phone_list)

类似这样的东西:

phone_list = [e.text for e in element.findall('Phone//')]
person.append(phone_list)

还是它(我认为是最佳选择):

person.append([e.text for e in element.findall('Phone//')])

因此,您将能够绕过该错误并显着减少代码量:)