shelve模块 xml模块
# 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模块的更多相关文章
- 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 ...
- 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时间 ...
- 常用模块:re ,shelve与xml模块
一 shelve模块: shelve模块比pickle模块简单,只有一个open函数,所以使用完之后要使用f.close关闭文件.返回类似字典的对象,可读可写;key必须为字符串,而值可以是pytho ...
- python configparse模块&xml模块
configparse模块 用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser. [DEFAULT] serveraliveinterval = ...
- python笔记7 logging模块 hashlib模块 异常处理 datetime模块 shutil模块 xml模块(了解)
logging模块 日志就是记录一些信息,方便查询或者辅助开发 记录文件,显示屏幕 低配日志, 只能写入文件或者屏幕输出 屏幕输出 import logging logging.debug('调试模式 ...
- logging模块、shutil模块、subprocess模块、xml模块
logging模块 shutil模块 subprocess模块 xml模块 logging模块 函数式简单配置 import logging logging.debug('debug message' ...
- [xml模块、hashlib模块、subprocess模块、os与sys模块、configparser模块]
[xml模块.hashlib模块.subprocess模块.os与sys模块.configparser模块] xml模块 XML:全称 可扩展标记语言,为了能够在不同的平台间继续数据的交换,使交换的数 ...
- 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 ...
- Python(文件、文件夹压缩处理模块,shelve持久化模块,xml处理模块、ConfigParser文档配置模块、hashlib加密模块,subprocess系统交互模块 log模块)
OS模块 提供对操作系统进行调用的接口 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname") 改变当前脚本工作目 ...
随机推荐
- Window7系统安装Ubuntu16双系统
在电脑上插入ubuntu系统启动盘,之前做好的u盘启动盘,重启计算机,进入BIOS设置界面,设置系统启动为u盘启动,保存后退出.之后进入ubuntu系统安装界面. 在安装界面中选择系统语言,选择安装u ...
- python实现RESTful服务(基于flask)
https://www.jianshu.com/p/6ac1cab17929 http://www.pythondoc.com/flask/quickstart.html 在java中调用python ...
- CentOS7设置启动模式问题
参考地址 https://www.linuxidc.com/Linux/2015-12/126356.htm
- 从 spring-cloud-alibaba-nacos-config 进入 nacos-client
sc 的 bootstrap context 是 main application context 的 parent,需要在 main application context 中使用的 bean 可以 ...
- Python解决ModuleNotFoundError: No module named 'Queue'的问题
我们知道Python2和Python3两个版本之间,有些不兼容的地方,Python3中引入Queue会报出这个问题. Python3中要这样引入: import queue Python2中要这样引入 ...
- [Python3] 006 列表的常用方法
目录 一个篱笆三个桩,list 有--好多个桩 1. 列表的小伙伴们 (1) 召唤小伙伴 (2) 我给"他们"分了个组 2. 小伙伴们的"才艺展示" (1) & ...
- Java-集合第四篇Queue集合
1.什么是Queue 模拟队列数据结构,先进先出(FIFO),从队尾加元素,从队头取元素. 2.Queue接口中定义了如下几个方法: 1>void add(Object o):将指定元素 ...
- Java数组的使用
一.数组的动态初始化 1.声明数据类型[] 数组名;或数据类型 数组名[];2.开辟空间数组名 = new 数据类型[长度];//长度必不可少3.手动赋值数组名[下标] = 值;4.使用(打印.运算. ...
- httplib模块:(一个相对底层的http请求模块)
httplib是一个相对底层的http请求模块,期上有专门的包装模块,如urllib内建模块,goto第三方模块,但是封装的越高就约不灵活,比如urllib模块里的请求错误是就不会返回结果页的内容,只 ...
- 关于python - 更优雅的技巧
枚举 不要这么做: i = 0 for item in iterable: print i, item i += 1 而是这样: for i, item in enumerate(iterable): ...