I have a python script that parses an XML file that contains part information as well as a command on what the script should do with the information.
<?xml version="1.0"?>
<servicexml>
<action>
<command>Add</command>
<partnumber>1223</partnumber>
<partname>Battery</partname>
<partdescription>Holds charge</partdescription>
</action>
<action>
<command>Add</command>
<partnumber>4444</partnumber>
<partname>Pump</partname>
<partdescription>Pumps stuff</partdescription>
</action>
</servicexml>
I am attempting to write a python script that will strip out all relevant information and put it into a dictionary so I can look up values by key. Currently my code can only hold a single dimension – this means that in my XML file the last tag is the only value written. How Can I dynamically allocate dimensions in my dictionary to hold multiple actions from my XML file? This way I can access key values for each part sequence.
Here is my Python code
from lxml import etree
from StringIO import StringIO
actionInformation = []
tagsOfInterest = ['command','partnumber','partname','partdescrip']
tagDataOfIntrest = {}
xmlFile = "parts.xml"
context = etree.iterparse(xmlFile)
for action, elem in context:
if elem.tag in tagsOfInterest:
actionInformation.append([elem.tag,elem.text])
tagDataOfInterest = dict(actionInformation)
print tagDataOfInterest
SOLUTION Based on David Alber’s Answer
I had to change some of the imported modules however his method was still used.
from lxml import etree
xmlFile = "parts.xml"
context = etree.parse(xmlFile)
actions = context.findall('action')
parsed = [{field.tag: field.text for field in action} for action in actions]
Here is an approach that works. It does not make the
partnumberelements integers, but it does not look like you were doing that anyway. It would not be much trouble to modify it for that, though.Fully self-contained example
Here is a fully contained example to allow easy verification. The difference is that
xmlFilefrom above has been replaced with aStringIOobject.After running this, you can do