# import shelve
# f=shelve.open('db.shl')
# # f['stu1']={'name':'alex1','age':28}
# # f['stu2']={'name':'alex2','age':18}
# print(f['stu1']) # {'name': 'alex1', 'age': 28}
# print(f['stu1']['name']) # alex1
# f.close() '''
<?xml version="1.0"?>
<data v="1.0">
xxxxx
<country name="Liechtenstein">
<rank updated="yes">2</rank>
<year>2008</year>
<gdppc>141100</gdppc>
<neighbor name="Austria" direction="E"/>
<neighbor name="Switzerland" direction="W"/>
</country>
<country name="Singapore">
<rank updated="yes">5</rank>
<year>2011</year>
<gdppc>59900</gdppc>
<neighbor name="Malaysia" direction="N"/>
</country>
<country name="Panama">
<rank updated="yes">69</rank>
<year>2011</year>
<gdppc>13600</gdppc>
<neighbor name="Costa Rica" direction="W"/>
<neighbor name="Colombia" direction="E"/>
</country>
</data>
''' # from xml.etree import ElementTree
# tree=ElementTree.parse('a.xml')
#
# root=tree.getroot()
# print(root.tag)
# print(root.attrib)
# print(root.text)
'''
data
{'v': '1.0'} xxxxx
'''
#三种查找方式
#从子节点找 找一个
# root.find()
#
# 从树形结构中查找
# root.iter() # from xml.etree import ElementTree
# tree=ElementTree.parse('a.xml')
# root=tree.getroot()
# print(root.find('country'))
# print(root.findall('country'))
'''
<Element 'country' at 0x00000255D4EEEE58>
[<Element 'country' at 0x00000255D4EEEE58>, <Element 'country' at 0x00000255D4EF3098>, <Element 'country' at 0x00000255D4EF3278>]
'''
# from xml.etree import ElementTree
# tree=ElementTree.parse('a.xml')
# root=tree.getroot()
# print(list(root.iter('rank')))
'''
[<Element 'rank' at 0x0000025ED931EE08>, <Element 'rank' at 0x0000025ED9323048>, <Element 'rank' at 0x0000025ED9323278>]
'''
# print(root.findall('country'))
# from xml.etree import ElementTree
# tree=ElementTree.parse('a.xml')
# root=tree.getroot()
# for country in root.findall('country'):
# print(country)
'''
<Element 'country' at 0x000002647FD7DE58>
<Element 'country' at 0x000002647FD83098>
<Element 'country' at 0x000002647FD83278>
'''
# from xml.etree import ElementTree
# tree=ElementTree.parse('a.xml')
# root=tree.getroot()
# for country in root.findall('country'):
# rank=country.find('rank')
# print(rank)
'''
<Element 'rank' at 0x0000015FEED8DE58>
<Element 'rank' at 0x0000015FEED93098>
<Element 'rank' at 0x0000015FEED932C8>
'''
# from xml.etree import ElementTree
# tree=ElementTree.parse('a.xml')
# root=tree.getroot()
# for country in root.findall('country'):
# rank=country.find('rank')
# print(rank.tag,rank.attrib,rank.text)
'''
rank {'updated': 'yes'} 2
rank {'updated': 'yes'} 5
rank {'updated': 'yes'} 69
'''
# 遍历文档树
# from xml.etree import ElementTree
# tree=ElementTree.parse('a.xml')
# root=tree.getroot()
# for item in root:
# print(item)
'''
<Element 'country' at 0x000001EA4A62CE58>
<Element 'country' at 0x000001EA4A633098>
<Element 'country' at 0x000001EA4A633278>
'''
# from xml.etree import ElementTree
# tree=ElementTree.parse('a.xml')
# root=tree.getroot()
# for country in root:
# print(country.tag)
'''
country
country
country
'''
# from xml.etree import ElementTree
# tree=ElementTree.parse('a.xml')
# root=tree.getroot()
# for country in root:
# print(country.attrib)
# print(country.attrib['name'])
# print('==>',country.attrib['name'])
'''
{'name': 'Liechtenstein'}
Liechtenstein
==> Liechtenstein
{'name': 'Singapore'}
Singapore
==> Singapore
{'name': 'Panama'}
Panama
==> Panama
'''
# from xml.etree import ElementTree
# tree=ElementTree.parse('a.xml')
# root=tree.getroot()
# for country in root:
# print('==>', country.attrib['name'])
# for item in country:
# print(item.tag,item.attrib,item.text)
'''
==> Liechtenstein
rank {'updated': 'yes'} 2
year {} 2008
gdppc {} 141100
neighbor {'name': 'Austria', 'direction': 'E'} None
neighbor {'name': 'Switzerland', 'direction': 'W'} None
==> Singapore
rank {'updated': 'yes'} 5
year {} 2011
gdppc {} 59900
neighbor {'name': 'Malaysia', 'direction': 'N'} None
==> Panama
rank {'updated': 'yes'} 69
year {} 2011
gdppc {} 13600
neighbor {'name': 'Costa Rica', 'direction': 'W'} None
neighbor {'name': 'Colombia', 'direction': 'E'} None
'''
# from xml.etree import ElementTree
# tree=ElementTree.parse('a.xml')
# root=tree.getroot()
# for year in root.iter('year'):
# print(year)
'''
<Element 'year' at 0x0000028C4CB9DF48>
<Element 'year' at 0x0000028C4CBA3188>
<Element 'year' at 0x0000028C4CBA33B8>
'''
# from xml.etree import ElementTree
# tree=ElementTree.parse('a.xml')
# root=tree.getroot()
# for year in root.iter('year'):
# print(year.tag,year.attrib,year.text)
'''
year {} 2008
year {} 2011
year {} 2011
'''
# from xml.etree import ElementTree
# tree=ElementTree.parse('a.xml')
# root=tree.getroot()
# for year in root.iter('year'):
# year.set('updated','yes')
# year.text=str(int(year.text)+1)
# tree.write('a.xml') # from xml.etree import ElementTree
# tree=ElementTree.parse('a.xml')
# root=tree.getroot()
# for country in root:
# obj=ElementTree.Element('egon')
# obj.attrib={'name':'egon','age':'18'}
# obj.text='egon is good'
# country.append(obj)
# tree.write('a.xml') # from xml.etree import ElementTree
# tree=ElementTree.parse('a.xml')
# root=tree.getroot()
# for rank in root.iter('rank'):
# if int(rank.text) == 5:
# obj=ElementTree.Element('egon')
# obj.attrib={'name':'egon','age':'18'}
# obj.text='egon is good'
# rank.append(obj)
# tree.write('a.xml')

shelve模块 xml模块的更多相关文章

  1. python 常用模块 time random os模块 sys模块 json & pickle shelve模块 xml模块 configparser hashlib subprocess logging re正则

    python 常用模块 time random os模块 sys模块 json & pickle shelve模块 xml模块 configparser hashlib  subprocess ...

  2. s14 第5天 时间模块 随机模块 String模块 shutil模块(文件操作) 文件压缩(zipfile和tarfile)shelve模块 XML模块 ConfigParser配置文件操作模块 hashlib散列模块 Subprocess模块(调用shell) logging模块 正则表达式模块 r字符串和转译

    时间模块 time datatime time.clock(2.7) time.process_time(3.3) 测量处理器运算时间,不包括sleep时间 time.altzone 返回与UTC时间 ...

  3. 常用模块:re ,shelve与xml模块

    一 shelve模块: shelve模块比pickle模块简单,只有一个open函数,所以使用完之后要使用f.close关闭文件.返回类似字典的对象,可读可写;key必须为字符串,而值可以是pytho ...

  4. python configparse模块&xml模块

    configparse模块 用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser. [DEFAULT] serveraliveinterval = ...

  5. python笔记7 logging模块 hashlib模块 异常处理 datetime模块 shutil模块 xml模块(了解)

    logging模块 日志就是记录一些信息,方便查询或者辅助开发 记录文件,显示屏幕 低配日志, 只能写入文件或者屏幕输出 屏幕输出 import logging logging.debug('调试模式 ...

  6. logging模块、shutil模块、subprocess模块、xml模块

    logging模块 shutil模块 subprocess模块 xml模块 logging模块 函数式简单配置 import logging logging.debug('debug message' ...

  7. [xml模块、hashlib模块、subprocess模块、os与sys模块、configparser模块]

    [xml模块.hashlib模块.subprocess模块.os与sys模块.configparser模块] xml模块 XML:全称 可扩展标记语言,为了能够在不同的平台间继续数据的交换,使交换的数 ...

  8. python day 9: xlm模块,configparser模块,shutil模块,subprocess模块,logging模块,迭代器与生成器,反射

    目录 python day 9 1. xml模块 1.1 初识xml 1.2 遍历xml文档的指定节点 1.3 通过python手工创建xml文档 1.4 创建节点的两种方式 1.5 总结 2. co ...

  9. Python(文件、文件夹压缩处理模块,shelve持久化模块,xml处理模块、ConfigParser文档配置模块、hashlib加密模块,subprocess系统交互模块 log模块)

    OS模块 提供对操作系统进行调用的接口 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname")  改变当前脚本工作目 ...

随机推荐

  1. JavaScript method overload

     https://stackoverflow.com/questions/2187666/help-with-js-and-functions-parameters JavaScript doesn' ...

  2. centos 6.9 mysql 安装配置

    1.全新系统,安装mysql yum -y install mysql mysql-server mysql-devel 2.启动mysql service mysqld start 3.修改密码 登 ...

  3. ajax传递json参数

    var pros = []; for(var i = 1; i <= 2; i++) { var obj = {}; obj.id = i; obj.age = i*20; pros = pro ...

  4. 二十六、python中json学习

    1.json序列介绍:提供4个关键字:dumps,dump,loads,load(与pickle用法完全相同) 语法:f.write(bytes(json.dumps(dict),encoding=& ...

  5. MySQL-default设置

    Both statements insert a value into the phone column, but the first inserts a NULL value and the sec ...

  6. debian sftp/ssh

    检查是否安装poenssh dpkg --get-selections | grep openssh 如下表示已经安装

  7. 【ABAP系列】SAP ABAP实现发送外部邮件(添加附件)功能

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP ABAP实现发送外部邮件(添 ...

  8. 20190909 SpringBoot集成Swagger

    SpringBoot集成Swagger 1. 引入依赖 // SpringBoot compile('org.springframework.boot:spring-boot-starter-web' ...

  9. java 历年版本特征(简化)

     无论是学习任何新知识,我都是从历史的角度着手,这让我对这些新知识是如何孕育出来的,有一个很好的理解 只从java 5-8,之后我也开始转nodejs了,感谢java伴随的这几年 Java5的新特性 ...

  10. Java中的容器(集合)

    1.Java常用容器:List,Set,Map List: 继承了Collection接口(public interface List<E> extends Collection<E ...