0

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

Can anyone help me out?

1
  • Without any additional information about the structure of your XML files there is no reasonable answer to your question. Commented Feb 10, 2016 at 8:23

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

root = ET.parse('your_data.xml').getroot() this fails for very large XML files, as they cannot be loaded into memory

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.