python模块汇总练习
模块练习
1、random模块
# print(random.random())
# print(random.randint(1,3)) #模拟随机验证码
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))
2、模拟打印进度条
num=30
print('%s%%'%num) #打印进度条
width=50
print(('[%%-%ds]'%width)%'#')
print(('[%%-%ds]'%width)%'##')
print(('[%%-%ds]'%width)%'###')
print(('[%%-%ds]'%width)%'####') def progress(percent,width=50):
if percent > 1:
percent=1
show_str=(('[%%-%ds]'%width)%(int(width*percent)* '#'))
print('\r%s %d%%'%(show_str,int(100*percent)),end=' ') import time,random
recv_size=0
total_size=8097
while recv_size<total_size:
time.sleep(0.5)
recv_size+=1024
percent=recv_size / total_size
progress(percent)
3、shelve模块(序列化)
import shelve #序列化
info1={'age':18,'herght':180,'weight':80}
info2={'age':88,'herght':150,'weight':80} d=shelve.open('db.shv')
d['egon']=info1
d['alex']=info2
d.close() #反序列化
d=shelve.open('db.shv')
print(d['alex'])
print(d['egon'])
d.close() #修改数据
d=shelve.open('db.shv',writeback=True)
# d['alex']['age']=10000
print(d['alex'])
d.close()
4、xml模块
import xml.etree.ElementTree as ET
tree=ET.parse('a.xml')
root=tree.getroot() #========================================》》查
#三种方式查找节点
res=root.iter('rank')#会在整个树中进行查找,而且是查找所有
for item in res:
# print(item)
print('='*50)
print(item.tag)#标签名
print(item.attrib)#属性
print(item.text)#文本内容 res=root.find('country')#只能在当前元素的下一级进行查找,并且只找到一个就截止
# print(res.tag)
# print(res.attrib)
# print(res.text)
n=res.find('rank')
print(n.attrib) res=root.findall('country')#只能在当前元素的下一级进行查找,
print([item.tag for item in res]) #=====================================》》改
import xml.etree.ElementTree as ET
tree=ET.parse('a.xml')
root=tree.getroot() for year in root.iter('year'):
year.text=str(int(year.text)+10)
year.attrib={'updated':'yes'}
tree.write('a.xml') #===============================》增
import xml.etree.ElementTree as ET
tree=ET.parse('a.xml')
root=tree.getroot() for country in root.iter('country'):
# print('year')
year=country.find('year')
# print(year)
if int(year.text) > 2010:
# print(country.attrib)
# ele=ET.Element('egon')
# ele.attrib={'nb':'yes'}
# ele.text='好'
# country.append(ele)
country.remove(year) tree.write('b.xml')
2018-08-15 22:54:40
python模块汇总练习的更多相关文章
- Python模块汇总
正则模块:re 日期和时间模块:datetime 和time模块 加密模块:hashlib 远程连接模块:paramiko 日志模块:logging 高级函数工具包:functools 多线程: 队列 ...
- 2013流行Python项目汇总
2013流行Python项目汇总 转自:http://www.kankanews.com/ICkengine/archives/102963.shtml Python作为程序员的宠儿,越来越得到人们的 ...
- Python资源汇总
Python 目录: 管理面板 算法和设计模式 反垃圾邮件 资产管理 音频 验证 构建工具 缓存 ChatOps工具 CMS 代码分析和Linter 命令行工具 兼容性 计算机视觉 并发和并行性 组态 ...
- 大话python模块与包
前言 眼看着老掌门年纪越来越大,掌门之位的传承也成了门派中的一件大事.这天,老掌门把小掌门叫到跟前,语重心长地说道:孩子啊,以后你就要继任掌门之位了,我就传授此生所学的绝世功法与你,以后可要悉心学习, ...
- Xilinx 常用模块汇总(verilog)【03】
作者:桂. 时间:2018-05-10 2018-05-10 21:03:44 链接:http://www.cnblogs.com/xingshansi/p/9021919.html 前言 主要记 ...
- 老猿Python博文汇总目录--按标题排序
☞ ░ 前往老猿Python博文目录 ░ 本部分为老猿CSDN全部博文的汇总(含转载部分),所有文章在此未进行归类,仅按文章标题排序,方便关键字查找.本部分内容将至少以周为单位定期更新,可能不包含发布 ...
- Python模块Ⅰ
Python模块Ⅰ part1 模块的定义/取别名 自定义模块 什么是模块:模块的本质就是.py文件,封装语句的最小单位 模块中出现的变量,for循环,if结构,函数定义...称为模块成员 模块的运行 ...
- 使用C/C++写Python模块
最近看开源项目时学习了一下用C/C++写python模块,顺便把学习进行一下总结,废话少说直接开始: 环境:windows.python2.78.VS2010或MingW 1 创建VC工程 (1) 打 ...
- Python模块之configpraser
Python模块之configpraser 一. configpraser简介 用于处理特定格式的文件,其本质还是利用open来操作文件. 配置文件的格式: 使用"[]"内包含 ...
随机推荐
- 内核的执行头程序head.S
功能 定义data段和text段 重新手动初始化gdt表, idt表, tss表结构 初始化页表和页目录 --> 页目录的数据放在一个页表中 在页目录中, 其实地址为0x1000, 初始化页目录 ...
- svn报错cleanup failed–previous operation has not finished; run cleanup if it was interrupte...
今天在svn提交的时候它卡顿了一下,我以为已经提交完了,就按了一下,结果就再也恢复不了,也继续不了了... 报错 cleanup failed–previous operation has not f ...
- 《C#高效编程》读书笔记11-理解短小方法的优势
我们最好尽可能的编写最清晰的代码,将优化交给JIT编译器完成.一个常见的错误优化是,将大量逻辑放在一个函数中,以期减少额外的方法调用开销.这种将函数逻辑直接写在循环内部的常见优化做法却会降低.NET应 ...
- Ecshop如何解决Deprecated: preg_replace()报错
今天安装Ecshop后,运行出现各种问题,其中 Deprecated: preg_replace() 之类的报错最多,下面贴出解决方案: 错误原因: preg_replace() 函数中用到的修饰符 ...
- 使用AOP监控用户操作并插入数据库
引入依赖 <!--spring切面aop依赖--> <dependency> <groupId>org.springframework.boot</group ...
- Router模块
一.应用场景 监听浏览器地址栏URL的hash值(#后面的部分)的变化,用正则匹配出参数执行相应的JS方法.URL地址的hash部分充当业务逻辑的分发单位. 示例: <!DOCTYPE html ...
- 子元素的margin-top会影响父元素
---恢复内容开始--- 之前在写项目的时候,发现原本想让父子元素之间加点边距,却让父元素产生了margin-top,于是百度之后发现了原因. 在css2.1盒模型中 In this specific ...
- 链接文字<a>保持原有的字体颜色
<style type="text/css"> #red {color: red;} #blue {color: blue;} #orange {color: oran ...
- java.lang.ClassNotFoundException:org/apache/commons/collections/CursorableLinkedList
明明有 commons-collections.jar 将jar包复制到Tomcat的WEB-INF/lib下就可以了...
- C#基础知识图谱