您错过了本教程中非常重要的一行
import xml.etree.ElementTree as xml
这使得xml.etree.ElementTree现在在整个模块中都称为xml。
我碰巧有python 2.5.4,并且我已经验证了与上面相同的代码可以正常工作:
user@Comp test$ cat test.py import xml.etree.ElementTree as xmlroot = xml.Element('root')child = xml.Element('child')root.append(child)child.attrib['name'] = "Charlie"file = open("test.xml", 'w')xml.ElementTree(root).write(file)file.close()user@Comp test$ /usr/bin/python2.5 --versionPython 2.5.4user@Comp test$ /usr/bin/python2.5 test.py user@Comp test$ cat test.xml <root><child name="Charlie" /></root>user@Comp test$因此,请检查并确保您正在运行python 2.5.4,并尝试重新安装。问题不在于它是python
2.5.4还是您的代码。这是一些安装问题,您正在运行其他版本的python,或者还有其他一些奇怪的问题。



