0

I want to convert a very large XML file into CSV format without hardcoding tagnames.

Can anyone help me out?

Insane Skull
  • 9,220
  • 9
  • 44
  • 63
prasanna777
  • 11
  • 1
  • 4

1 Answers1

2

Firstly, you need to parse your XML files. This can be done via ElementTree API:

Example code:

import xml.etree.ElementTree as ET
root = ET.parse('your_data.xml').getroot()
with open("output.csv", "w") as file:
    for child in root:
        print(child.tag, child.attrib)
        # naive example how you could save to csv line wise
        file.write(child.tag+";"+child.attrib)

There are also solutions to parse your XMLs directly as dictionary.

Then csv.DictWriter can be used to save the dictionary as CSV.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Johann Hagerer
  • 1,048
  • 2
  • 10
  • 28