简体   繁体   中英

XML Element Tree Python Iterate through child and save each subchild as CSV column

I have been searching all over SO for a solution to my current issue but haven't been able to find anything that adequately solves it. I am attempting to iterate through the child of a root node in an XML document and pull the values of each of the subchilds within the iteration (eg iterate through in the XML below and pull each instance of COMPANY and ROLE). This is the last piece of a huge project and I am completely stuck, any and all help would be immensely appreciated.

<Personnel Personnel ID = "123">
  <First_Name> First </First_Name>
  <Last_Name> Last </Last_Name>
  <User_ID> 123 </User_ID> 
  <Date> 2017-01-01 </Date>
  <INFO>
      <INFO_1>
        <PHONE> 555-555-5555 </PHONE>
      <INFO_2>
        <EMAIL> name@email.com </EMAIL>
  </INFO>     
  <LINKS>
      <LINK COMPANY = "Company 1" ROLE = "Role 1" />
      <LINK COMPANY = "Company 2" ROLE = "Role 2" />
      <LINK COMPANY = "Company 3" ROLE = "Role 3" />
       ....
      <LINK Company = "Company n" ROLE = "Role n" />
  </LINKS>
  <TAGS>
      <TAG Term="Tag 1" />
      <TAG Term="Tag 2" />
      <TAG Term="Tag 3" />
      ...................
      <TAG Term="Tag n" />
  </Tags>
  <Personnel_Field_1> Field 1 </Personnel_Field_1>
  <Personnel_Field_2> Field 2 </Personnel_Field_2>

Example Code:

 for contact in root.findall('Personnel'):
    Personnel_ID = contact.get('Personnel_ID')  
    contact_info.append(Personnel_ID)   

    First_Name = contact.find('First_Name').text
    contact_info.append(First_Name)

    Last_Name = contact.find('Last_Name').text
    contact_info.append(Last_Name)

    User = contact.find('User_ID').text
    contact_info.append(User)

    Date = contact.find('Date').text
    contact_info.append(Date)

    Email = contact.find( './/EMAIL' ).text
    contact_info.append(Email)

    Phone = contact.find( './/PHONE' ).text
    contact_info.append(Phone)

    Personnel_1 = contact.find('Personnel_Field_1').text.encode('utf-8')
    contact_info.append(Personnel_1)

    Personnel_2 = contact.find('Personnel_Field_2').text.encode('utf-8')
    contact_info.append(Personnel_2)

So far I have been successful in pulling the following and saving them into CSV columns: Personnel ID, First Name, Last Name, User ID, Date, Email, Phone, Personnel 1, Personnel 2

What I am stuck on is the ability to iterate through to parse COMPANY and ROLE, as well as to parse each term. I need to save each company, role, and tag value as their own columns as well. If anyone can help by simply showing me how to iterate through these elements, I will be able to save them down into CSV columns.

Thanks in advance for any and all advice this is the last piece of a huge project I am working on and I feel like I've exhausted all potential solutions that I have found.

Simply add nested for loops to parse the LINK and TAG children.

for contact in root.findall('Personnel'):
    ...
    for link in contact.findall('.//LINK'):
        contact_info.append(link.get('COMPANY'))
        contact_info.append(link.get('ROLE'))

    for tag in contact.findall('.//TAG'):
        contact_info.append(tag.get('Term'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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