Day13 Python基础之time/datetime/random模块一(十一)
time模块
import time
print(help(time))
time.time() #return current time in seconds since the Epoch as a float 时间戳:以秒的形式返回当前时间,从1970年算起
time.clock() #return CPU time since process start as a float 只计算CPU执行的时间
time.sleep() # delay for a number of seconds given as a float
time.gmtime() # convert seconds since Epoch to UTC tuple 结构化时间(UTC英国格林尼治天文台为零时区,北京在东八区,时差为8 个小时)
time.localtime() #convert seconds since Epoch to local time tuple(本地时间)结构化时间
time.strftime() # convert time tuple to string according to format specification 格式化时间
Commonly used format codes: %Y Year with century as a decimal number.
%m Month as a decimal number [01,12].
%d Day of the month as a decimal number [01,31].
%H Hour (24-hour clock) as a decimal number [00,23].
%M Minute as a decimal number [00,59].
%S Second as a decimal number [00,61].
%z Time zone offset from UTC.
%a Locale's abbreviated weekday name.
%A Locale's full weekday name.
%b Locale's abbreviated month name.
%B Locale's full month name.
%c Locale's appropriate date and time representation.
%I Hour (12-hour clock) as a decimal number [01,12].
%p Locale's equivalent of either AM or PM. 实例:
import time
print(time.strftime('%Y--%m--%d %H:%M:%S '))
输出结果:
2018--06--13 11:52:04 <class 'str'>
strftime(format)
time.strptime((string, format)) #parse string to time tuple according to format specification字符串时间转化成格式化时间
b=time.strptime('2018--06--13 12:02:03','%Y--%m--%d %H:%M:%S')
print(b)
print(b.tm_year)
print(b.tm_min)
输出结果:
time.struct_time(tm_year=2018, tm_mon=6, tm_mday=13, tm_hour=12, tm_min=2, tm_sec=3, tm_wday=2, tm_yday=164, tm_isdst=-1)
2018
2
strptime(string,format)
time.ctime(second) #Convert a time in seconds since the Epoch to a string in local time.把秒的时间转换成格式化本地时间
print(time.ctime(123456))
输出结果:
Fri Jan 2 18:17:36 1970
ctime()
time.mktime() #Convert a time tuple in local time to seconds since the Epoch. #把本地时间转换成秒
print(time.mktime(time.localtime()))
输出结果:
1528863479.0
mktime()
datetime模块
import datetime
1 import datetime
2 print(datetime.datetime.now())
3 输出结果:
4 2018-06-13 12:21:02.344843
datetime
random模块
import random
random.random() #x in the interval [0, 1).
random.randint(1,8) #int x in inteval[1,8]
random.randrange(1,8) #int x in inteval[1,8)
random.choice(sequence) #Choose a random element from a non-empty sequence.
random.sample(sequence,count) #Choose count random elements from a non-empty sequence.
# 验证码函数
import random
def v_code():
code=''
for i in range(5):
add_num=random.randrange(0,9)
add_al=chr(random.randrange(65,91))
# if random.randint(1,2)==1:
# code+=str(add_num)
# else:
# code+=add_al
code+=random.choice([str(add_num),add_al])
print(code) v_code()
随机产生验证码实例
注:可以通过chr把数字转换成相应的字母
最新文章
- MVC 请求处理流程(一)
- wininet异步InternetReadFile和超时相关问题
- springmvc之前后台传值
- iOS开发UI篇—在UITableview的应用中使用动态单元格来完成app应用程序管理界面的搭建
- HDU 4717 The Moving Points (三分法)
- TP验证
- mysql下sql语句 update 字段=字段+字符串
- Geoserver 相关学习
- [html][转]常用返回顶部代码
- 正确理解javascript的this关键字
- Hadoop: failed on connection exception: java.net.ConnectException: Connection refuse
- 激活IDEA
- Oracle12c中PL/SQL(DBMS_SQL)新特性之隐式语句结果(DBMS_SQL.RETURN_RESULT and DBMS_SQL.GET_NEXT_RESULT)
- nginx配置ssl验证
- altiumdesigner的基本你操作
- Json数据产生树形结构
- hadoop执行 报错
- ChinaCock界面控件介绍-CCNewsSilder
- 2017-2018-2 《网络对抗技术》 20155322 Exp 5 MSF基础应用
- java 实现验证码功能