88112

XML to csv(-like) format

Question:

I have an XML file, which I would like to convert / export to a csv(-like) format.

Example:

<root> <child> <Name>John</Name> <Surname>Doe</Surname> <Name>George</Name> <Surname>Washington</Surname> </child> </root>

ATM I do it like this (the print is for debug, nothing finished yet):

#!/bin/python env import xml.etree.ElementTree as etree tree = etree.parse('./foobar.xml') root = tree.getroot() elements = ('Name', 'Surname') for i in elements: for i in root.iter(i): print(i.text)

<strong>Current Output:</strong>

John George Doe Washington

<strong>The result I would like to get:</strong>

John, Doe George, Washington

Can anyone give me a hand here?

Thank you very much.

Answer1:

Use xpath and zip().

#!python import xml.etree.ElementTree as etree root = etree.parse('./foobar.xml').getroot() for a in zip(root.findall("child/Name"), root.findall("child/Surname")): print(", ".join([x.text for x in a]))

If you want to call flexible with names of elements.

#!python import xml.etree.ElementTree as etree child = etree.parse('./foobar.xml').getroot().find('child') elements = ('Name', 'Surname') for a in zip(*[child.findall(x) for x in elements]): print(", ".join([x.text for x in a]))

Recommend

  • Remove Google Maps iframe address popup
  • How to access attribute value in xml containing namespace using ElementTree in python
  • How are and tags different from ? Do they affect anything? [closed]
  • Using Python's ast to get attributes from a class
  • Python get XML siblings into dictionary
  • Can I parse xpath using python, selenium and lxml?
  • XML to csv(-like) format
  • for-each and for loops
  • How to get data from an XML file with xmlns in root
  • python ElementTree the text of element who has a child
  • Angular: $resource update fire PUT request on second call only
  • How to parse an XML file with encoding declaration in Python?
  • CakePHP 2.x Custom Route Pagination
  • Trying to get Geolocation on a map element in windows phone 8 using emulator
  • Validate an HTML fragment using html5lib
  • Windows 10 - System.Speech.Synthesis cannot get Mobile version of voice
  • lxml.objectify and leading zeros
  • Host name does not match the certificate subject
  • Echo specific rows from a mysql database
  • lxml removing tags when parsing?
  • How to sort an array considering localization?
  • Python: xml.etree.ElementTree.ParseError: not well-formed (invalid token)
  • Laravel / Eloquent WHERE NOT SUBQUERY
  • Can someone please explain how this implementation of bucket sort works?
  • Where to put clearQueue in jQuery code
  • Wait for .each() .getJSON request to finish before executing a callback
  • Creating a Multidimensional, Associative Array in VBScript
  • Do query loads all the data in memory
  • nodemcu custom firmware build problems
  • Android Studio Can't Find tools.jar
  • Convert SQLite database to XML
  • Eclipse CDT error: Unable to compile
  • Can I check if a recipient has an automatic reply before I send an email?
  • Getting last autonumber in access
  • align graphs with different xlab
  • Return words with double consecutive letters
  • -fvisibility=hidden not passed by compiler for Debug builds
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Reading document lines to the user (python)
  • Python/Django TangoWithDjango Models and Databases