zoukankan      html  css  js  c++  java
  • 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>


  • 相关阅读:
    [译]为什么你要学Go?
    类Lisp解释器JavaScript实现
    前端同学Windows中生存指北
    Virtualbox
    数据发布和上线日报&周报系统开发总结
    [Lab1]五分钟了解Makefile
    解决linux下解压来自windows的zip文件产生乱码的问题
    Arch使用过程中出现的问题汇总
    jQuery为什么移除了.toggle()方法
    Linux配置DNS
  • 原文地址:https://www.cnblogs.com/zhangsenzhen/p/9406638.html
Copyright © 2011-2022 走看看