Parse an XML API response into a Python dictionary so you can access nested fields and attributes like any other Python data.
Process a large XML data dump using streaming mode to avoid loading the entire file into memory.
Round-trip XML: read an XML config file, modify values as a Python dict, then write it back as valid XML.
Convert legacy SOAP or RSS XML responses into Python dictionaries for easier data processing.
xmltodict is a Python library that converts XML documents into Python dictionaries, making XML data easier to work with in code. XML is an older data format that uses angle-bracket tags to organize information, while a Python dictionary is a simpler key-value structure that most Python code already knows how to handle. The library bridges the two by parsing an XML document and producing a dictionary that mirrors its structure. The conversion follows a predictable set of rules. XML element names become dictionary keys. Repeated elements at the same level become a list. XML attributes are stored in the same dictionary with an @ prefix so they are easy to spot and separate from element content. Text content inside an element is stored under a key called #text. This consistent mapping means you can navigate the resulting dictionary the same way you would navigate any other Python data structure. The library also supports going the other direction. You can call its unparse function on a dictionary and get back a valid XML document. This makes it possible to read an XML file, modify the data as a dictionary, and write it back out as XML without using a heavier XML processing library. For large XML files, such as data dumps from Wikipedia or similar sources, the library includes a streaming mode. Instead of loading the entire file into memory at once, it calls a function you provide for each item at a specified depth in the document. This allows processing very large XML files without running out of memory. Installation is through pip. The library is small, written in pure Python, and has no required dependencies beyond the standard library's built-in XML parser. It supports XML namespaces, with options to expand them, collapse them to short prefixes, or ignore them entirely.
← martinblech on gitmyhub — every repo by this author, as a profile.
Verify against the repo before relying on details.