python - Retrieve value of nested tag and store it in nested dictionary -
i’m trying store values between xml tags list , it’s not working. i’m having trouble wrapping head around this. following code gives attributeerror: 'list' object has no attribute 'find'
here code:
import xml.etree.elementtree et tree = et.parse(‘//storage1//harison/dev/gis/cocalig.xml’) root = tree.getroot() input['merchandise_db'] = {'host': root.findall('cali_merchandise_db_ref').find('host').text, 'db_name': root.findall('cali_merchandise_db_ref').cali_merchandise_db_ref.find('db_name').text, 'schema': root.findall('cali_merchandise_db_ref').cali_merchandise_db_ref.find('schema').text, 'user': root.findall('cali_merchandise_db_ref').cali_merchandise_db_ref.find('user').text, 'pwd': root.findall('cali_merchandise_db_ref').cali_merchandise_db_ref.find('password').text} input['incident_db'] = {'host': root.findall('incident_db_ref').incident_db_ref.find('host').text, 'db_name': root.findall('incident_db_ref').incident_db_ref.find('db_name').text, 'schema': root.findall('incident_db_ref').incident_db_ref.find('schema').text, 'user': root.findall('incident_db_ref').incident_db_ref.find('user').text, 'pwd': root.findall('incident_db_ref').incident_db_ref.find('password').text} input['projection_db'] = {'host': root.findall('cali_projection_db_ref').incident_db_ref.find('host').text, 'db_name': root.findall('cali_projection_db_ref').incident_db_ref.find('db_name').text, 'schema': root.findall('cali_projection_db_ref').incident_db_ref.find('schema').text, 'user': root.findall('cali_projection_db_ref').incident_db_ref.find('user').text, 'pwd': root.findall('cali_projection_db_ref').incident_db_ref.find('password').text}
i understand why code gives error not sure how fix it. before when outputted value screen iterated using #for cali_merchandise_db_ref in root.findall('cali_merchandise_db_ref'): and
cali_merchandise_db_ref.find(‘xyz’).text`
tl;dr
given xml file
<animal> <lastmodified_date>4/6/2015</lastmodified_date> <data>some junk goes here</data> <dog> <sound>woof</sound> <hastail>yes</hastail> </dog> <cat> <sound>meow</sound> <hastail>yes</hastail> </cat> </animal>
how get
input['dog']['sound'] = 'woof' input['cat']['sound'] = 'meow' input['dog']['hastail'] = 'yes' input['cat']['hastail'] = 'yes'
failed attempt #2:
>>> import xml.etree.elementtree et >>> open ("cocalig.xml", "r") myfile: ... data=myfile.read().replace('\n', '') ... >>> root = et.fromstring(data) >>> mydic = {} >>> element in root: ... host_tag = element.find("host") ... mydic[element.tag] = {"host": host_tag.text} ... traceback (most recent call last): file "<stdin>", line 3, in <module> attributeerror: 'nonetype' object has no attribute 'text' >>>
edited fit adjusted example
your example can solved this:
root = et.fromstring("<animal> <lastmodified_date>4/6/2015</lastmodified_date><data>some junk goes here</data><dog><sound>woof</sound><hastail>yes</hastail></dog><cat><sound>meow</sound><hastail>yes</hastail></cat></animal>") mydic = {} element in root: if element.tag in ('lastmodified_date', 'data'): mydic[element.tag] = element.text else: # here come elements describing animals mydic[element.tag] = {} animal_element in element: # "sound" , "hastail" in undefined order mydic[element.tag][animal_element.tag] = animal_element.text
this generates following dictionary:
{'lastmodified_date': '4/6/2015', 'data': 'some junk goes here', 'dog': {'hastail': 'yes', 'sound': 'woof'}, 'cat': {'hastail': 'yes', 'sound': 'meow'}}
basically, don't need explicitly want extract "sound" , "hastail" animal. say: want sub-elements of animal. if want consider missing tags error, you'd have explicitly check them:
else: # here come elements describing animals mydic[element.tag] = {} try: sound = element.find("sound") mydic[element.tag][sound.tag] = sound.text except: # error handling
you can solve problem in manner too. chaining findall
, find
calls might sound intuitive, findall returns list of found elements on searching works different. instead, iterate on elements.
Comments
Post a Comment