How can i convert an xml file into JSON using python?

前端 未结 3 1614
[愿得一人]
[愿得一人] 2021-01-18 04:12

I have an XML file which I want to convert into JSON file using python, but its nt working out for me.



    

        
3条回答
  •  温柔的废话
    2021-01-18 04:57

    Another option is xmltodict (full disclosure: I wrote it). It can help you convert your XML to a dict+list+string structure, following this "standard". It is Expat-based, so it's very fast and doesn't need to load the whole XML tree in memory.

    Once you have that data structure, you can serialize it to JSON:

    import xmltodict, json
    
    o = xmltodict.parse(' text text ')
    json.dumps(o) # '{"e": {"a": ["text", "text"]}}'
    

提交回复
热议问题