shelve模块 xml模块

import shelve
f = shelve.open(r'shelve') #新建shelve.dat,shelve.bak,shelve.dir三个文件 目的:将一个字典放入文本 f={}
f["name"] = ("zsz","alex","sb")
f["age"] = 18
f.close()
f = shelve.open(r'shelve')
print(f.get("name")[1]) ---> alex
print(f["name"][0]) ---> zsz
import xml.etree.ElementTree as ET    
tree = ET.parse("xml_lesson")
root = tree.getroot()
print(root.tag)
#遍历xml文档
for child in root:
print(child.tag,child.attrib)
for i in child:
print(i.tag,i.text,i.attrib)
#只遍历year节点
for node in root.iter('year'):
print(node.tag,node.text)
#修改
for node in root.iter('year'):
new_year = int(node.text) + 1
node.text = str(new_year)
node.set("updated","yes")
tree.write("xml_lesson.xml")
#删除node
for country in root.findall('country'): #找到root下所有标签为country的
rank = int(country.find('rank').text)
if rank > 50:
root.remove(country)
tree.write("xml_lesson.xml")
xml_lesson.xml
<data>
    <country name="Liechtenstein">
        <rank updated="yes">2</rank>
        <year>2018</year>
        <gdppc>141100</gdppc>
        <neighboor name="Austria" direction="E"/>
        <neighboor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank updated="yes">5</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighboor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank updated="yes">69</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighboor name="Costa Rica" direction="W"/>
        <neighboor name="Colombia" direction="E"/>
    </country>


原文地址:https://www.cnblogs.com/zhangsenzhen/p/9406638.html