Parsing Solr XML into Python Dictionary -


I am new to Python and am trying to pass a xml document (filled with documents for a solr instance) in a Python dictionary. . I'm really having trouble trying to accomplish this. I have tried to use Elementary and MiniDom but I can not get the right result.

Here is my XML structure:

  & lt; Add & gt; & Lt; Doc & gt; & Lt; Area name = "Janlight" & gt; 45.639 968 & lt; / Field & gt; & Lt; Field name = "carofficehours" & gt; 2000-01-010 9: 00: 00.000Z & lt; / Field & gt; & Lt; Field Name = "Janmandity" & gt; 5.879745 & lt; / Field & gt; & Lt; / Doc & gt; & Lt; Doc & gt; & Lt; Area name = "Janlight" & gt; 46.639 968 & lt; / Field & gt; & Lt; Field name = "carofficehours" & gt; 2000-01-010 9: 00: 00.000Z & lt; / Field & gt; & Lt; Field name = "birthland" & gt; 6.879745 & lt; / Field & gt; & Lt; / Doc & gt; & Lt; / Add & gt;   

And this should change me to a dictionary that looks like this:

  Doctor {"genLatitude": '45.66 968 ',' CarofficeHourend ':' 2000-01-01 -09: 00: 00.000J ',' gene ligititude ':' 5.879745 ',}   

How do I work, but how do I work All "docs" in the dictionary

Cheers.

  Pprint import pprint root = etree.fromstring (xmlstr) # or etree.parse (filename_or_file) ) Import as xml.etree.cElementTree .getroot () docs = [[f.attrib ['name']: for f.text f (doctor 'doc')] for doctor in doc.iterfind ('field [@name] '}})   

output
  [[CarofficesHoursend:' 2000-01-01 -09: 00: 00.000Z ',' Janlitid ': '45.66 9 68', 'Transmission': '5.879745'}, {CaroffisHours and ':' 2000-01-010 9: 00: 00.000Z ',' Gene Lititude ': '46. 639 9 68 ',' gene longitude ':' 6.879745 '}]   

where xmlstr is:

  xmlstr = " "" & Gt; Add & gt; & lt; Doc & gt; Area Name = "Junketty" & gt; 45.639 968 & lt; / Field & gt; & lt; Fee Name Name = "Caroffhoushour End"> 2000-01-01 9: 00: 00.000Z  & lt; Doc & gt; Area Name = "Junketty" & gt; 46.639 968 & lt; / Field & gt; & lt; Field Name = "Caroffhoushour End" & gt; 2000-01-010 9: 00: 00.000Z    

Comments