python - Beautifulsoup xml -
i trying use code add xml tree simple info, have in table. each file has id need add it. corresp dictionary has file name , id couples. there empty element in xml, called idno[@type='tm'] in need enter corresponding id number.
from bs4 import beautifulsoup dir = 'files/' corresp = {"00100004":"362375", "00100005":"362376", "00100006":"362377", "00100007":"362378"} filename, tm in corresp.iteritems(): soup = beautifulsoup(open(dir + filename + ".xml")) tmid = soup.find("idno", type="tm") tmid.append(tm) print soup
my first problem some time works, time says
tmid.append(tm) attributeerror: 'nonetype' object has no attribute 'append'
i have no idea why. yesterday evening run same sample code , complains in way.
i have tried etree
import xml.etree.elementtree et dir = 'files/' corresp = {"00100004":"362375", "00100005":"362376", "00100006":"362377", "00100007":"362378"} filename, tm in corresp.iteritems(): f = open(dir + filename + ".xml") tree = et.parse(f) tmid = tree.findall("//idno[@type='tm']") tmid.append(tm) tree.write('output.xml', encoding='utf-8', xml_declaration=true)
but says "no element found: line 1, column 0"
my second, related problem when did work, not able write output file. ideally write file modifying.
thank advise on this.
for first question:
find()
returns result. if find()
can’t find anything, returns none. both result , none not python list, not have append()
method.
check doc: http://www.crummy.com/software/beautifulsoup/bs4/doc/
Comments
Post a Comment