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来操作文件. 配置文件的格式: 使用"[]"内包含 ...
随机推荐
- htaccess转换httpd.ini方法及案例参考
案例1:httpd.ini适合IIS使用,.htaccess适合Apache使用,nginx.conf适合Nginx使用 转换前:httpd.ini [ISAPI_Rewrite] # 3600 = ...
- jquery的.get方法说解
·Customer类 public class Customer { public int Unid { get; set; } public string CustomerName { get; s ...
- Windows8 64位运行Silverlight程序不能访问WCF的解决方案
公司的项目是Silverlight+WCF,而我的本本是Win8 64位系统,一直无法正常运行Silverlight程序,一个同事找到了方案,现分享出来 一种情况是,Vs2010运行程序时,报无法加载 ...
- Python 加持,给你更有趣的 Azure 虚拟机开关重启方法!
在程序员的世界里,有关编程语言孰优孰劣的争论从来就没有消停过,不管你更粉哪种语言,毫无疑问,每种语言都有自己擅长的领域,而一些语言因为上手简单.扩展性强.功能强大等因素,往往会比较多地出现在我们面前, ...
- hdu-1317 XYZZY---Floyd判连通+bellman最短路
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1317 题目大意: 题意:有n个房间(n<=100),每个房间有一个点权(第1号房间和第n号房间 ...
- WARNING: The TCP backlog setting of 511.解决
redis启动警告问题:WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/so ...
- centos 7 虚拟机启用网卡
1.vi /etc/sysconfig/network-scripts/ifcfg-enp0s3 2.编辑默认网卡配置文件,将ONBOOT由no改为yes,编辑完成后,按ESC回至命令模板,输入&qu ...
- Restful API 的设计规范
RESTful 是目前最流行的 API 设计规范,用于 Web 数据接口的设计.降低开发的复杂性,提高系统的可伸缩性. Restful API接口规范包括以下部分: 一.协议 API与用户的通信协议, ...
- 拷贝时间测试=cudamelloc+cudahostalloc
/* * Copyright 1993-2010 NVIDIA Corporation. All rights reserved. * * NVIDIA Corporation and its lic ...
- CentOS创建不需要输入密码的sudo用户
linux 怎么让普通用户使用sudo权限不需要输入密码. 在网上看到很多资料,都只说到了在/etc/sudoers添加下列行即可: username ALL=(ALL) ...