python:常用模块一
一、collections模块
collections = namedtuple (,[,=point(1,2(p)
2,deque: 双端队列,可以快速的从另外一侧追加和推出对象
=10)
q.put(56
双端队列:>>> from collections import deque >>> q = deque(['a','b','c']) >>> q.append('x') >>> q.appendleft('y') >>> q deque(['y','a','c','x']) 3,Counter: 计数器,主要用来计数
c = Counter(: 5,: 4,: 3,: 2,: 1})
4.OrderedDict: 有序字典
collections = OrderedDict([(,1),(,2),(,3(od)
(od[ k (k)
5.defaultdict: 带有默认值的字典 collections =[11,22,33,44,55,66,77,88,99,90= value value>66].append(value)
二、时间模块 time.sleep() time.time() 时间有三种标识方式: 时间戳、元组(struct_time)、格式化的时间字符串 1,时间戳 时间戳(timestamp) :通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量。我们运行“type(time.time())”,返回的是float类型。 2,格式化的时间字符串(Format String): ‘1999-12-06’ %y 两位数的年份表示(00-99%Y 四位数的年份表示(000-9999%m 月份(01-12%d 月内中的一天(0-31%H 24小时制小时数(0-23%I 12小时制小时数(01-12%M 分钟数(00=59%S 秒(00-59%%%%%%j 年内的一天(001-366%%U 一年中的星期数(00-53%w 星期(0-6%W 一年中的星期数(00-53%%%%% %号本身
3,元组(struct_time) :struct_time元组共有9个元素共九个元素:(年,月,日,时,分,秒,一年中第几周,一年中第几天等)
>>><span style="color: #008000">#<span style="color: #008000">时间戳
1500875844.800804 <span style="color: #008000">#<span style="color: #008000">时间字符串
<span style="color: #008000">#<span style="color: #008000">时间元组:localtime将一个时间戳转换为当前时区的struct_time 5,几种格式之间的转换 <div class="cnblogs_code"> 结构化时间
>>>time.gmtime(1500000000=2017,tm_mday=14,tm_hour=2,tm_min=40,tm_sec=0,tm_wday=4,tm_yday=195,tm_isdst=>>>time.localtime(1500000000=2017,tm_hour=10,tm_isdst=<span style="color: #008000">#<span style="color: #008000">结构化时间-->时间戳 <span style="color: #008000">
<span style="color: #008000">time.mktime(结构化时间)
字符串时间
time.strftime(
time.strftime(,time.localtime(1500000000
<span style="color: #008000"># <span style="color: #008000">字符串时间-->结构化时间<span style="color: #008000"> <span style="color: #008000">time.strptime(时间字符串,字符串对应格式)<span style="color: #800000">2017-03-16<span style="color: #800000">",<span style="color: #800000">"<span style="color: #800000">%Y-%m-%d<span style="color: #800000">"<span style="color: #000000">) time.struct_time(tm_year=2017,tm_mon=3,tm_mday=16,tm_hour=0,tm_min=0,tm_wday=3,tm_yday=75,tm_isdst=-1<span style="color: #000000">) >>>time.strptime(<span style="color: #800000">"<span style="color: #800000">07/24/2017<span style="color: #800000">",<span style="color: #800000">"<span style="color: #800000">%m/%d/%Y<span style="color: #800000">"<span style="color: #000000">) time.struct_time(tm_year=2017,tm_mday=24,tm_wday=0,tm_isdst=-1) %a %b %d %H:%M:%S %Y串
time.asctime(time.localtime(1500000000
<span style="color: #008000"># <span style="color: #008000">%a %d %d %H:%M:%S %Y串 --> 结构化时间<span style="color: #008000"> <span style="color: #008000">time.ctime(时间戳) 如果不传参数,直接返回当前时间的格式化串<span style="color: #800000">'<span style="color: #800000">Mon Jul 24 15:19:07 2017<span style="color: #800000">' time.ctime(1500000000<span style="color: #000000">) <span style="color: #800000">'<span style="color: #800000">Fri Jul 14 10:40:00 2017<span style="color: #800000">' 三、random模块 >>>
random.random()
0.7664338663654585
random.uniform(1,3)
1.6270147180533838
<span style="color: #008000"># <span style="color: #008000">随机整数<span style="color: #008000"> 大于等于1且小于等于5之间的整数 random.randrange(1,10,2) <span style="color: #008000">#<span style="color: #008000"> 大于等于1且小于10之间的奇数 <span style="color: #008000"># <span style="color: #008000">随机选择一个返回
<span style="color: #008000">#<span style="color: #008000">打乱列表顺序
四、OS模块 os模块是与操作系统交互的一个接口 'nt'; Linux->'posix'
os.system("bash command") 运行shell命令,直接显示
os.popen("bash command).read() 运行shell命令,获取执行结果
os.environ 获取系统环境变量
os.path 注意:os.stat st_mode: inode 保护模式
st_ino: inode 节点号。 st_dev: inode 驻留的设备。 st_nlink: inode 的链接数。 st_uid: 所有者的用户ID。 st_gid: 所有者的组ID。 st_size: 普通文件以字节为单位的大小;包含等待某些特殊文件的数据。 st_atime: 上次访问的时间。 st_mtime: 最后一次修改的时间。 st_ctime: 由操作系统报告的<span style="color: #800000">"<span style="color: #800000">ctime<span style="color: #800000">"。在某些系统上(如Unix)是最新的元数据更改的时间,在其它系统上(如Windows)是创建时间(详细信息参见平台的文档)。 五、sys 1
1(e)
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |