中国开发网: 论坛: 超级垃圾站: 贴子 665253
leejd
别人写的测试代码,但是在我的电脑上测试失败,怀疑是版本不一样导致的
确实是好东西,而且用起来非常方便,简单的写几行代码体验一下~~~
#Python2.4下的代码
import cElementTree as ET

#解析文件
tree = ET.parse('test.xml')

#获得根节点
root = tree.getroot()

#找到第一个tagformat标签
tag = root.find('tagformat')
#遍历所有的opt标签
for ele in tag.findall('opt'):
print ele.text

#获得属性
print root.get('name')
#修改或新建属性
root.set('user', 'liujunzhi')

#以utf-8编码保存
f = open('output.xml', 'w')
tree.write(f, encoding='utf-8')
f.close()

相关信息:


欢迎光临本社区,您还没有登录,不能发贴子。请在 这里登录