What's the best way to convert XML to a dictionary in Python? or How can I efficiently parse XML into

20 Replies, 1687 Views

If you’re allergic to external libs, here’s a hacky way with `ElementTree`:

```python
import xml.etree.ElementTree as ET
tree = ET.fromstring(xml_string)
data = {elem.tag: elem.text for elem in tree}
```

But it’s basic—won’t handle nesting or attributes well. xmltodict is better for that.

Messages In This Thread



Users browsing this thread: 1 Guest(s)