当前位置:
首页 > Python基础教程 >
-
random 模块、 shutil 模块、shelve模块、 xml模块
6.12 random 模块
print(random.random()) | (0,1)----float | 大于0且小于1之间的小数 |
---|---|---|
print(random.randint(1,3)) | [1,3] | 大于等于1且小于等于3之间的整数 |
print(random.randrange(1,3)) | [1,3) | 大于等于1且小于3之间的整数 |
print(random.choice ( [1,'23', [4,5] ] ) ) | 1或者23或者[4,5] | |
print(random.sample( [1,'23', [ 4,5 ] ] , 2 ) ) | 第二个参数是任意几个元素组合 | 列表元素任意2个组合 |
print(random.uniform(1,3)) | (1,3) | 大于1小于3的小数,如1.927109612082716 |
import random item=[1,3,5,7,9] random.shuffle(item) # 打乱item的顺序,相当于"洗牌" print(item)
6.121 生成随机验证码
import random def make_code(n=5): res='' for i in range(n): s1=str(random.randint(0,9)) s2=chr(random.randint(65,90)) res+=random.choice([s1,s2]) return res print(make_code(10))
6.13 shutil 模块
import shutil import time ret = shutil.make_archive( # 压缩 "day15_bak_%s" %time.strftime('%Y-%m-%d'), 'gztar', root_dir=r'D:\code\SH_fullstack_s1\day15' ) import tarfile # 解压 t=tarfile.open('day15_bak_2018-04-08.tar.gz','r') t.extractall(r'D:\code\SH_fullstack_s1\day16\解包目录') t.close()
6.14 shelve模块
shelve模块比pickle模块简单,只有一个open函数,返回类似字典的对象,可读可写 ;key必须为字符串,而值可以是python所支持的数据类型
import shelve info1={'age':18,'height':180,'weight':80} info2={'age':73,'height':150,'weight':80} d=shelve.open('db.shv') #增 d['egon']=info1 d['alex']=info2 d.close() d=shelve.open('db.shv') #查 print(d['egon']) #{'age': 18, 'height': 180, 'weight': 80} print(d['alex']) #{'age': 73, 'height': 150, 'weight': 80} d.close() d=shelve.open('db.shv',writeback=True) #改 d['alex']['age']=10000 print(d['alex']) #{'age': 10000, 'height': 150, 'weight': 80} d.close()
6.15 xml模块
xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单,在json还没诞生的黑暗年代,只能选择用xml,至今很多传统公司如金融行业的很多系统的接口还主要是xml
6.151 xml模块举例:
<data> <country name="Liechtenstein"> <rank updated="yes">2</rank> <year updated="yes">2018</year> <neighbor direction="E" name="Austria" /> <neighbor direction="W" name="Switzerland" /> </country> <country name="Singapore"> <rank updated="yes">5</rank> <year updated="yes">2021</year> <neighbor direction="N" name="Malaysia" /> </country> <country name="Panama"> <rank updated="yes">69</rank> <year updated="yes">2021</year> <neighbor direction="W" name="Costa Rica" /> <neighbor direction="E" name="Colombia" /> </country> </data>
查 : 三种查找节点的方式
import xml.etree.ElementTree as ET tree=ET.parse('a.xml') root=tree.getroot() res=root.iter('rank') # 会在整个树中进行查找,而且是查找到所有 for item in res: print(item) # <Element 'rank' at 0x000002C3C109A9F8>..... print(item.tag) # 标签名 rank rank rank print(item.attrib) # 属性 {'updated': 'yes'} {'updated': 'yes'}... print(item.text) # 文本内容 2 5 69 res=root.find('country') # 只能在当前元素的下一级开始查找。并且只找到一个就结束 print(res.tag) print(res.attrib) print(res.text) nh=res.find('neighbor') # 在res的下一级查找 print(nh.tag) print(nh.attrib) cy=root.findall('country') # 只能在当前元素的下一级开始查找, 但是查找到所有 print([item.attrib for item in cy]) #[{'name':'Liechtenstein'},{'name':'Singapore'},{'name':'Panama'}]
改:
import xml.etree.ElementTree as ET tree=ET.parse('a.xml') root=tree.getroot() res=root.iter('year') for item in res: item.text=str(int(item.text) + 10) item.attrib={'updated':'yes'} tree.write('a.xml') #把更改写入 tree.write('c.xml') #新建一个.xml文件,把更改的结果写入
增:
import xml.etree.ElementTree as ET tree=ET.parse('a.xml') root=tree.getroot() for country in root.iter('country'): year=country.find('year') if int(year.text) > 2020: ele=ET.Element('egon') ele.attrib={'nb':'yes'} ele.text='非常帅' country.append(ele) country.remove(year) tree.write('b.xml')
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比
一款纯 JS 实现的轻量化图片编辑器
关于开发 VS Code 插件遇到的 workbench.scm.
前端设计模式——观察者模式
前端设计模式——中介者模式
创建型-原型模式