# 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. 阿里云安装配置yarn,Nginx

    1.和npm 相比yarn 的优势在于 1.比npm快.npm是一个个安装包,yarn 是并行安装. 2.npm 可能会有情况 同样的 package.json 文件在不同的机器上安装的包不一样.导致 ...

  2. 取得所有网卡的MAC地址,包括禁用的

    先在nuget包中添加System.Management.Automation引用. 然后下面就是代码了. using System;using System.Collections.ObjectMo ...

  3. React-Native 之 GD (八)GET 网络请求封装

    1.到这里,相信各位对 React-Native 有所熟悉了吧,从现在开始我们要慢慢往实际的方向走,这边就先从网络请求这部分开始,在正式开发中,网络请求一般都单独作为一部分,我们在需要使用的地方只需要 ...

  4. __main__ — Top-level script environment

    w 29.4. __main__ — Top-level script environment — Python 3.6.1 documentation  https://docs.python.or ...

  5. php对bom的处理

    通常只有在windows的notepad中 , 创建文本文件, 保存为UTF-8 时, 它会自动添加3个字节: ef bb bf. 用editplus来看txt文件就可以看得很清楚. 但是, 只有wi ...

  6. PHP会话

    B/S请求响应模式是无状态的.任意的请求间不存在任何的联系,不能将请求状态保持下去. 会话技术可以给每个浏览器分配持久数据,这些数据不会随着一次请求和相应结束而销毁. COOKIE cookie是一种 ...

  7. PHP版本问题

    PHP 5.3以下版本 无法用下标直接取得函数返回的数组 eg: $val_0 = explode(',', $val)[0]//报错 #要改成: $exploded_val = explode(', ...

  8. 创建虚拟环境virtualenv的小问题

    在创建完虚拟环境后,settings里面虚拟环境的python编译器不是虚拟的,而是全局的,这个时候. 由于创建的虚拟环境的存储地址默认是在c盘. 自定义虚拟环境的存储地址步骤: 第一步:在配置环境变 ...

  9. 2019了,给自己立一个flag吧

    新年伊始,元旦已过,虽然有迟了,但是,相对于整年来说,还是比较早.年度总结,年度规划,除过上交的报告以外,还得自己给自己立个flag,一次来督促自己,而不是为了别的.做这些事,不仅仅是为了能更好的工作 ...

  10. 阿里云DNS api接口 shell 更改DNS解析

    可定时任务检查域名解析,调用alidns.sh更新DNS解析 #!/bin/bash # alidns.sh #https://www.cnblogs.com/elvi/p/11663910.html ...