Read XML file with Argparse and convert it to JSON

Asked

Viewed 42 times

-1

Hello! I have a question and I am very new in the subject: I need to create a Python code that reads a file XML and turn that same file into a JSON. I need to use the Argparse for that, but I’m not restricted to just that lib. Someone can give me that light?

1 answer

1


You are in luck! There is a module called xmltodict that does just that:

import xmltodict, json
with open('arquivo.xml') as f:
    d = xmltodict.parse(f.read())
with open('arquivo.json', 'w') as f:
    json.dump(d, f)

The module is here. Can be installed by pip also.

  • Thanks man! I am very new and I was suffering with it there. I already marked as reply!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.