博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python时间模块和random模块
阅读量:4709 次
发布时间:2019-06-10

本文共 3420 字,大约阅读时间需要 11 分钟。

模块:用一坨代码实现了某个功能的代码集合。

类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合。而对于一个复杂的功能,可能需要多个函数才能完成(函数又可以在不同的.py文件中),n个.py文件组成的代码集合就成为模块。

如:os是系统相关的模块;file是文件操作相关的模块。

模块分为三种:自定义模块、内置标准模块(又称标准库)、开源模块

time&datetime模块

#!/usr/bin/env python#-*- coding:utf-8 -*-#Author: Tony Cabelimport time,datetimeprint(time.clock())         #返回处理器时间,3.3开始已废弃 , 改成了time.process_time()                            # 测量处理器运算时间,不包括sleep时间,不稳定,mac上测不出来print(time.altzone/3600)    #打印时区print(time.asctime())       #返回时间格式"Mon Feb 20 15:01:02 2017",t=time.localtime()          #返回本地时间 的struct time对象格式print(time.localtime(time.time()+3*3600))   #本地时间后三个小时的时间print(t)print(t.tm_year,t.tm_yday)print(time.time())          #时间戳,从1970年一月一日至今的秒数print(time.gmtime())        #返回UTC时间print(time.ctime())         #返回Fri Aug 19 12:38:29 2016 格式,t2=time.strptime("2017-2-20 11:30","%Y-%m-%d %H:%M")    #将 日期字符串 转成 struct时间对象格式t2_stamp=time.mktime(t2)            #将struct时间对象转成时间戳print(t2)t3=time.localtime(t2_stamp)         #将本地时间戳转换成struct_time格式t3_str=time.strftime("%Y-%m-%d-%H-%M.log",t3)      #将utc struct_time格式转成指定的字符串格式print(t3_str)t4=datetime.datetime.now()          #返回当前时间,格式:2017-02-20 15:01:02.487076print(t4)t4_fr=t4-datetime.timedelta(days=3) #返回三天前的这个时候的时间,也可以小时分钟秒print(t4_fr)print(t4.replace(year=2016,month=3,hour=12))    #将当前时间进行任意替换

 时间转换路径:

 
Directive Meaning Notes
%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.  
%d Day of the month as a decimal number [01,31].  
%H Hour (24-hour clock) as a decimal number [00,23].  
%I Hour (12-hour clock) as a decimal number [01,12].  
%j Day of the year as a decimal number [001,366].  
%m Month as a decimal number [01,12].  
%M Minute as a decimal number [00,59].  
%p Locale’s equivalent of either AM or PM. (1)
%S Second as a decimal number [00,61]. (2)
%U Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0. (3)
%w Weekday as a decimal number [0(Sunday),6].  
%W Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0. (3)
%x Locale’s appropriate date representation.  
%X Locale’s appropriate time representation.  
%y Year without century as a decimal number [00,99].  
%Y Year with century as a decimal number.  
%z Time zone offset indicating a positive or negative time difference from UTC/GMT of the form +HHMM or -HHMM, where H represents decimal hour digits and M represents decimal minute digits [-23:59, +23:59].  
%Z Time zone name (no characters if no time zone exists).  
%% A literal '%' character.

 

random模块

import randomprint(random.random())      #打印一个随机小数,小数点后十六位print(random.randint(1,3))  #随机打印一个从1到3的数,包括3print(random.randrange(1,10)) #在range范围内打印一个随机数import stringsrc=string.ascii_letters+string.digits      #生成随机密码的一种方式print(''.join(random.sample(src,6)))checkcode=''                    #另一种生成随机验证码方式for i in range(6):    current=random.randint(0,6)    if current != i:        temp=chr(random.randint(65,90))    else:        temp=random.randint(0,9)    checkcode+=str(temp)print(checkcode)

 备注:用random.sample不会生成重复的字符,也就是说第一种方式生成的验证码中不会有重复的字符,而第二种方式生成的可能会有重复的字符。

转载于:https://www.cnblogs.com/caibao666/p/6419701.html

你可能感兴趣的文章
查看cpu 温度
查看>>
Unity 中使用预编译指令区分平台
查看>>
论坛项目感想
查看>>
WordPress版微信小程序3.5版发布
查看>>
sicily 1198 Substring
查看>>
Servlet 是否线程安全
查看>>
第二次冲刺(每天更新)
查看>>
Knockout应用开发指南之入门介绍
查看>>
转:国内智能音箱的隐忧:国外拼价格,国内又如何?
查看>>
Odoo Email Template Problem
查看>>
HashMap的源码以及原理
查看>>
Win10任务栏卡死解决方法
查看>>
批量修改文件的编码
查看>>
关于下拉框在页面加载时候选中值得问题
查看>>
SPRING IN ACTION 第4版笔记-第十章Hitting the database with spring and jdbc-002-本章的源代码...
查看>>
怎么教软件工程课的看法
查看>>
Timeout 时间已到。在操作完成之前超时时间已过或服务器未响应。
查看>>
获取两个时间之间的天数
查看>>
springboot 学习之路 2(注解介绍)
查看>>
vue项目引入自定义.css的样式文件
查看>>