for k, v in items:
    if isinstance(k, QName):
        k = k.text
if isinstance(v, QName):
    v = qnames[v.text]
else:
    v = _escape_attrib(v, encoding)
write(' %s="%s"' % (qnames[k], v))
Порядок настройки атрибутов Elementtree