zoukankan      html  css  js  c++  java
  • shelve模块和xml模块

    shelve模块

    shelve模块比pickle模块简单,只有一个open,返回类似字典的对象,可读可写:key必须为字符串,

    而值可以是python所支持的数据类型

    import shelve
    f=shelve.open(r'shelve.txt')
     f['stu1_info']={'name':'egon','age':18,'hobby':['piao','smoking','drinking']}
     f['stu2_info']={'name':'gangdan','age':53}
     f['school_info']={'website':'http://www.pypy.org','city':'beijing'}
    print(f['stu1_info']['hobby'])
    f.close()

     xml模块

    xml是实现不同语言或者程序之间进行数据交换的协议,跟json差不多,但是json使用起来更简单,不过、

    ,古时候,在json还没诞生的黑暗年代,大家只能选择用xml呀,至今很多传统公司如金融行业的很多系统

    的接口还主要是xml

    xml的格式如下,就是通过<>节点来区别数据结构的:

    #==========================================>查
    import xml.etree.ElementTree as ET
    #
    # tree=ET.parse('a.xml')
    # root=tree.getroot()
    #三种查找节点的方式
    # res=root.iter('rank') # 会在整个树中进行查找,而且是查找到所有
    # for item in res:
    #     # print(item)
    #     print('='*50)
    #     print(item.tag) # 标签名
    #     print(item.attrib) #属性
    #     print(item.text) #文本内容
    
    
    # res=root.find('country') # 只能在当前元素的下一级开始查找。并且只找到一个就结束
    # print(res.tag)
    # print(res.attrib)
    # print(res.text)
    # nh=res.find('neighbor')
    # print(nh.attrib)
    
    
    # cy=root.findall('country') # 只能在当前元素的下一级开始查找,
    # print([item.attrib for item in cy])
    
    
    
    
    #==========================================>改
    # import xml.etree.ElementTree as ET
    # tree=ET.parse('a.xml')
    # root=tree.getroot()
    #
    # for year in root.iter('year'):
    #     year.text=str(int(year.text) + 10)
    #     year.attrib={'updated':'yes'}
    #
    #
    # # tree.write('b.xml')
    # tree.write('a.xml')
    
    
    
    #==========================================>增
    import xml.etree.ElementTree as ET
    tree=ET.parse('a.xml')
    root=tree.getroot()
    
    for country in root.iter('country'):
        # print(country)
        year=country.find('year')
        # print(year)
        if int(year.text) > 2020:
            # print(country.attrib)
            # ele=ET.Element('egon')
            # ele.attrib={'nb':'yes'}
            # ele.text='非常帅'
            # country.append(ele)
            country.remove(year)
    tree.write('b.xml')

    自己创建xml文档

    import xml.etree.ElementTree as ET
    
    new_xml = ET.Element("namelist")
    name = ET.SubElement(new_xml,"name",attrib={"enrolled":"yes"})
    age = ET.SubElement(name,"age",attrib={"checked":"no"})
    sex = ET.SubElement(name,"sex")
    sex.text = '33'
    name2 = ET.SubElement(new_xml,"name",attrib={"enrolled":"no"})
    age = ET.SubElement(name2,"age")
    age.text = '19'
     
    et = ET.ElementTree(new_xml) #生成文档对象
    et.write("test.xml", encoding="utf-8",xml_declaration=True)
     
    ET.dump(new_xml) #打印生成的格式
  • 相关阅读:
    基于 Spring Cloud 的微服务架构实践指南(上)
    1分钟,带你上手“Markdown”语法
    Spring Cloud 理论篇
    如何设计一个优雅的RESTFUL的接口
    简简单单之Linux命令入门
    SpringBoot基础架构篇3(Redis)
    SpringBoot基础架构篇1(SpringBoot、MyBatis-Plus与Thymeleaf)
    基础之Lambda和Stream的邂逅
    浏览器如何执行JS
    渲染进程里的线程
  • 原文地址:https://www.cnblogs.com/wuchenyu/p/8761435.html
Copyright © 2011-2022 走看看