博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

python入门_10

Posted on 2017-03-01 22:53  天高鹿苑  阅读(91)  评论(0)    收藏  举报

内建模块

1.关于时间的模块

 time 和 datatime

在python中,通常有这3种方法来表示时间:

         1. 时间戳 也就是秒数

         2. 格式化的时间字符串

         3. 元组(struct_time)共9个元素

由于python的time模块实现主要是调用C库,所以各个平台可能有所不同。

 

常识普及:UTC(Coordinated Universal Time,世界协调时)亦即格林威治天文时间,世界标准时间。在中国为UTC+8。DST(Daylight Saving Time)即夏令时。

 

 

时间戳(timestamp)的方式:通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量;why 1970? UNIX系统认为1970年1月1日0点是时间纪元。

运行“type(time.time())”,返回的是float类型。返回时间戳方式的函数主要有time(),clock()等。

代码插入:

import time
print(time.time())
print(type(time.time()))

1488369896.9040778  #时间戳表示法

<class 'float'>

 

Process finished with exit code 0

 

 

元组(struct_time)方式:struct_time元组共有9个元素,返回struct_time的函数主要有gmtime(),localtime(),strptime()。下面列出这种方式元组中的几个元素:

 

import time
print(time.time())
print(type(time.time()))
print(time.clock()) #返回处理器时间,3.3开始已废弃,改成time.process_time()测量处理器运算时间,不包括
print(time.process_time())
print(time.asctime()) #返回时间格式为:Wed Mar  1 20:10:23 2017
print(time.localtime())#返回本地时间的struct time对象格式
print(time.gmtime(time.time()-800000))#返回utc时间对象格式
print(time.asctime(time.localtime()))#返回时间格式为:"Fri Aug 19 11:14:16 2016"
print(time.ctime())#返回Fri Aug 19 12:38:29 2016 格式, 同上

 

时间格式转换

 

 

import time
#日期字符串 转成 时间戳
str=time.strptime("2016/01/02","%Y/%m/%d")
print(str) #struct时间对象格式
str2 = time.mktime(str)#将struct时间对象转换成时间戳
print(str2)

#将时间戳转为字符串格式
print(time.gmtime(time.time()-86640))#将utc时间戳转换成struct_time格式
print(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime()))#将utc struct_time格式转成指定的字符串格式

datetime 时间的加减

import datetime,time
print(datetime.datetime.now())#返回 2017-03-01 20:26:07.007886
print(time.time()) #返回时间戳
print(datetime.date.fromtimestamp(time.time())) #将时间戳改为日期格式 2017-03-01
print(datetime.datetime.now() )# 返回2017-03-01 20:29:12.583336
print(datetime.datetime.now() + datetime.timedelta(3)) #当前时间+3天 2017-03-04 20:30:03.875143
print(datetime.datetime.now() + datetime.timedelta(-3))#当前时间-3天  2017-02-26 20:30:27.744159
print(datetime.datetime.now() + datetime.timedelta(hours=3)) #当前时间+3小时 2017-03-01 23:30:57.958662
print(datetime.datetime.now() + datetime.timedelta(minutes=30)) #当前时间+30分

时间替换

import time ,datetime

c_time  = datetime.datetime.now()
print(c_time.replace(minute=3,hour=2)) #时间替换 2017-03-01 02:03:09.164937
'''%a    本地(locale)简化星期名称
%A    本地完整星期名称
%b    本地简化月份名称
%B    本地完整月份名称
%c    本地相应的日期和时间表示
%d    一个月中的第几天(01 - 31)
%H    一天中的第几个小时(24小时制,00 - 23)
%I    第几个小时(12小时制,01 - 12)
%j    一年中的第几天(001 - 366)
%m    月份(01 - 12)
%M    分钟数(00 - 59)
%p    本地am或者pm的相应符    一
%S    秒(01 - 61)    二
%U    一年中的星期数。(00 - 53星期天是一个星期的开始。)第一个星期天之前的所有天数都放在第0周。    三
%w    一个星期中的第几天(0 - 6,0是星期天)    三
%W    和%U基本相同,不同的是%W以星期一为一个星期的开始。
%x    本地相应日期
%X    本地相应时间
%y    去掉世纪的年份(00 - 99)
%Y    完整的年份
%Z    时区的名字(如果不存在为空字符)
%%    ‘%’字符
'''

2. random模块

随机模块

#!/usr/bin/env python
#_*_encoding: utf-8_*_
import random
print (random.random())  #0.6445010863311293
#random.random()用于生成一个0到1的随机符点数: 0 <= n < 1.0
print (random.randint(1,7)) #4
#random.randint()的函数原型为:random.randint(a, b),用于生成一个指定范围内的整数。
# 其中参数a是下限,参数b是上限,生成的随机数n: a <= n <= b
print (random.randrange(1,10)) #5
#random.randrange的函数原型为:random.randrange([start], stop[, step]),
# 从指定范围内,按指定基数递增的集合中 获取一个随机数。如:random.randrange(10, 100, 2),
# 结果相当于从[10, 12, 14, 16, ... 96, 98]序列中获取一个随机数。
# random.randrange(10, 100, 2)在结果上与 random.choice(range(10, 100, 2) 等效。
print(random.choice('liukuni')) #i
#random.choice从序列中获取一个随机元素。
# 其函数原型为:random.choice(sequence)。参数sequence表示一个有序类型。
# 这里要说明一下:sequence在python不是一种特定的类型,而是泛指一系列的类型。
# list, tuple, 字符串都属于sequence。有关sequence可以查看python手册数据模型这一章。
# 下面是使用choice的一些例子:
print(random.choice("学习Python"))#
print(random.choice(["JGood","is","a","handsome","boy"]))  #List
print(random.choice(("Tuple","List","Dict")))   #List
print(random.sample([1,2,3,4,5],3))    #[1, 2, 5]

 

import random,string

#随机整数
print(random.randint(0,99))
#随机选取0-100间的偶数:
print(random.randrange(0,101,2))

#随机浮点数
print(random.random())
print(random.uniform(1,10)) #有范围的浮点数

#多个字符中选取特定数量的字符:
print(random.sample('abcdefghijk',3))

#随机选取字符串:
print(random.choice(['apple','pear','peach','orange']))

#洗牌
items = [1,2,3,4,5,6,7]
print(items)
random.shuffle(items)
print(items)#可以看出打乱了列表的顺序

 

import random
print(random.random())#随机数从0,1之间选取一个浮点数 # 0.600728768207753
print(random.randint(1,7)) #1 random.randint()的函数原型为:random.randint(a, b),用于生成一个指定范围内的整数。
# 其中参数a是下限,参数b是上限,生成的随机数n: a <= n <= b

print(random.randrange(1,10))
# 函数原型为random.randrange([start], stop[, step]),还可以加步长,例如:
print(random.randrange(10,100,10)) #从10开始 顾头不顾尾法则,到90结束 10为步长,所以范围是10,20,30 ...90

print(random.choice(range(10,100,10))) #等价于 上面 random.randrange(10,100,10)

print(random.choice("人生苦短我用python")) #随机从字符串中选取一个字符返回

print(random.choice(['alex is ','my sir','heheda','zuile ']))#随机从列表中选取一项返回

print(random.choice(('hu','zhen','dong'))) #随机从元组中选一项返回

print(random.sample([1,2,3,4,5],3))  #[1, 2, 5]
print(random.sample(('liu','de','hua'),2))
#random.sample的函数原型为:random.sample(sequence, k),
# 从指定序列中随机获取指定长度的片断。sample函数不会修改原有序列。