码迷,mamicode.com
首页 > 其他好文 > 详细

xml处理模块

时间:2018-06-14 01:01:30      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:color   sub   find   test   none   span   get   coding   dump   

假如有下面这样简单的xml内容:

<?xml version="1.0" encoding="UTF-8"?>
<data>
    <person name="LiPanHui">
        <age>28</age>
        <sex>man</sex>
        <like name="WuTao"/>
    </person>
    <person name="XuQianHua">
        <age>30</age>
        <sex>man</sex>
        <like name="ChenWeiMeng"/>
    </person>
</data>

读文档的内容

>>> import xml.etree.ElementTree as ET                                                                                  
>>> tree = ET.parse("test.xml")
>>> root = tree.getroot()
>>> print(root.tag)
data

>>> for child in root: # 遍历xml文档
... print(child.tag, child.attrib)
... for i in child:
... print(i.tag, i.text)
...
person {name: LiPanHui}
age 28
sex man
like None
person {name: XuQianHua}
age 30
sex man
like None

>>> for node in root.iter(age): # 只遍历age节点
... print(node.tag, node.text)
...
age 28
age 30

修改和删除xml文档的内容

# 修改
>>> tree = ET.parse("test.xml")                                                                                         
>>> root = tree.getroot()
>>> for node in root.iter(age):
... new_age = int(node.text) + 1
... node.text = str(new_age)
... node.set(updated, yes)
...
>>> tree
<xml.etree.ElementTree.ElementTree object at 0x000001B5F29BCD68>
>>> tree.write(test2.xml)

新生成了文件test2.xml

<data>
    <person name="LiPanHui">
        <age updated="yes">29</age>
        <sex>man</sex>
        <like name="WuTao" />
    </person>
    <person name="XuQianHua">
        <age updated="yes">31</age>
        <sex>man</sex>
        <like name="ChenWeiMeng" />
    </person>
</data>
# 删除
>>> tree = ET.parse("test.xml") >>> root = tree.getroot() >>> for person in root.findall(person): ... age = int(person.find(age).text) ... if age < 29: ... root.remove(person) ... >>> tree.write("test.xml")

文件test.xml被修改成:

<data>
    <person name="XuQianHua">
        <age>30</age>
        <sex>man</sex>
        <like name="ChenWeiMeng" />
    </person>
</data>

新增xml文件

>>> new_xml = ET.Element("hobbies")
>>> name = ET.SubElement(new_xml, "singing", attrib={very_like: yes})
>>> is_usually = ET.SubElement(name, "is_usually")
>>> is_usually.text = yes
>>> name2 = ET.SubElement(new_xml, "play_game", attrib={very_like: no})
>>> is_usually = ET.SubElement(name2, "is_usually")
>>> is_usually.text = no
>>> et = ET.ElementTree(new_xml)
>>> et.write("test.xml", encoding="utf-8", xml_declaration=True)
>>> ET.dump(new_xml)# 打印生成的内容
<hobbies><singing very_like="yes"><is_usually>yes</is_usually></singing><play_game very_like="no"><is_usually>no</is_usually></play_game></hobbies>

生成的内容:

<?xml version=‘1.0‘ encoding=‘utf-8‘?>
<hobbies>
    <singing very_like="yes">
        <is_usually>yes</is_usually>
    </singing>
    <play_game very_like="no">
        <is_usually>no</is_usually>
    </play_game>
</hobbies>

 

xml处理模块

标签:color   sub   find   test   none   span   get   coding   dump   

原文地址:https://www.cnblogs.com/allenzhang-920/p/9180608.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!