I have a text string similar to the one below:
statistics:
time-started: Tue Feb 5 15:33:35 2013
time-sampled: Thu Feb 7 12:25:39 2013
statistic:
active: 0
interactive: 0
count: 0
up:
packets: 0
bytes: 0
down:
packets: 0
bytes: 0
I need to parse strings such as the one above (the strings I need to parse are actually much larger/deeper, here I just provided an example). The easiest way to parse out some elements I think would be to convert this string to an XML string and use xml.etree.ElementTree to choose the element I am looking for.
So I would like to convert the string above into an XML string like the one below:
<statistics>
<time-started>Tue Feb 5 15:33:35 2013</time-started>
<time-sampled>Thu Feb 7 12:25:39 2013</time-sampled>
<statistic>
<active>0</active>
<interactive>0</interactive>
</statistic>
<count>0</count>
<up>
<packets>0</packets>
<bytes>0</bytes>
</up>
<down>
<packets>0</packets>
<bytes>0</bytes>
</down>
</statistics>
As you can see all of the information is available in the string to convert it into an XML.
I don´t want to reinvent the wheel if there is a simple way or a module that can do this.
You are basically trying to convert YAML to XML. You can use PyYAML for parsing your input string to python dict and then use an xml generator to convert the dict to XML.