The set
method is the appropriate way to set attributes in ElementTree, and I just tested that it works when applied to an XML file read from disk.
I wonder if your problem is algorithmic--- the algorithm you wrote doesn't look like it does what you want. The idPOS
, idxml
, and POSval
are going to be equal to the last matching values in each file and w
is going to be equal to the last <w>
tag. It can only change one word, the last one. If you're going to be setting part of speech attributes in bulk, perhaps you want something more like the following (you may need to tweak the it if I've made some wrong assumptions about how POSfile
is structured):
# load all "pos" attributes into a dictionary for fast lookup
posDict = {}
for w in rootPOS.iter("w"):
if w.get("pos") is not None:
posDict[w.text] = w.get("pos")
# if we see any matching words in the xmlfile, set their "pos" attrbute
for w in root.iter("w"):
if w.text in posDict:
w.set("pos", posDict[w.text])