# 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. Hibernate入门简介

    什么是Hibernate框架? Hibernate是一种ORM框架,全称为 Object_Relative DateBase-Mapping,在Java对象与关系数据库之间建立某种映射,以实现直接存取 ...

  2. Ajax初探

    一.AJAX准备知识:JSON 1.stringify与parse方法 2.和XML的比较 二.AJAX简介 AJAX常见应用情景 AJAX的优缺点 优点: 三.jQuery实现的AJAX $.aja ...

  3. 搭建python-flask开发环境

    ubuntu环境 1. 更新系统软件源: 没有通过更新系统软件源的话,可能无法通过apt-get install安装我们需要用到的软件: $ sudo apt-get update $ sudo ap ...

  4. SpringBoot 切换国际化

    git:https://github.com/xiaozhuanfeng/demoProj 代码结构: application.properties: spring.messages.basename ...

  5. PANIC: Missing emulator engine program for ‘x86’ CPU.

    参考链接:https://zhidao.baidu.com/question/652153765084187325.html 解决方案:看图最上面路径,进入你的文件夹下,把红文件夹 ( 1 ) 中的所 ...

  6. linux下安装python27 nginx 和uwsgi

    注意: python27 默认没有安装 pip 和setuptools所以要提前安装.(务必先提前安装python27 哈 ) wget --no-check-certificate https:// ...

  7. 一些输出、处理细节&注意点

    https://blog.csdn.net/qq_41071646/article/details/79953476 输出百分比的时候,结果需要加上一个EPS(1e-6)四舍五入保证精度. 卡精度—— ...

  8. thead tbody tfoot

    <!DOCTYPE html> <html lang="en"> <head> <title>Title</title> ...

  9. 手把手教你用Pytorch-Transformers——实战(二)

    本文是<手把手教你用Pytorch-Transformers>的第二篇,主要讲实战 手把手教你用Pytorch-Transformers——部分源码解读及相关说明(一) 使用 PyTorc ...

  10. [Python3] 019 函数:确认过参数,返回对的值

    目录 0. 函数简介 1. 初识函数 2. 函数的参数与返回值 少废话,上例子 3. 查找函数的帮助文档 4. 函数的参数 (1) 参数分类 (2) 结构介绍 1) 普通参数 2) 默认参数 3) 关 ...