XML to dict(d)
Stefan Behnel
stefan_ml at behnel.de
Mon Jan 31 09:37:59 EST 2011
Daniel Stender, 31.01.2011 15:14:
>>> I've found that there is the library python-dictdlib for concatenating
>>> dict dictionaries, what would
>>> be the best way to "de-XML" the source file?
>>
>> How do you want to the dict to look like?
>
> What's in<key1> should be the "search word", the rest altogether belonging to that in a single line
> (with some minor modifications).
"the rest" isn't very precise, but here's an example of what you could do.
from xml.etree.cElementTree import iterparse
words = {}
h_tags = ('H1', 'H2', 'H3')
for _, element in iterparse('thefile.xml'):
if element.tag in h_tags:
words[element.findtext('h/key1')] = element
Since you didn't provide enough information, I have no idea what you want
to make of the "h", "body" and "tail" tags. But I'm sure you'll figure it out.
Stefan
More information about the Python-list
mailing list