Python: Append two XML tags -
i have xml file , i'd read data out of using python's xml.etree :
<a> <b> <authorname> <givenname>john</givenname> <familyname>smith</familyname> </authorname> <authorname> <givenname>saint</givenname> <givenname>patrick</givenname> <familyname>thomas</familyname> </authorname> </b> </a> the result wish have :
john smith saint patrick thomas the thing, may have noticed, have 1 givenname tag , have 2 givenname tags
what did :
from xml.etree import elementtree et xx = et.parse('file.xml') authorname = xx.findall('.//authorname') name in authorname: print(name[0].text + " " + name[1].text) it works fine 1 givenname tag not when have 2.
what can do?
thanks!
try this:
from xml.etree import elementtree et xx = et.parse('file.xml') authorname = xx.findall('.//authorname') name in authorname: namestr = ' '.join([child.text child in name]) print(namestr) you have @ child tags inside authorname, take text , join them namestr.
Comments
Post a Comment