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把数字转换成相应的字母
Day13 Python基础之time/datetime/random模块一(十一)的更多相关文章
- Python常用模块time & datetime &random 模块
时间模块前言 在Python中,与时间处理有关的模块就包括:time,datetime 一.在Python中,通常有这几种方式来表示时间: 时间戳 格式化的时间字符串 元组(struct_time)共 ...
- Py修行路 python基础 (二十)模块 time模块,random模块,hashlib模块,OS及sys模块
一.前提介绍: 可以开辟作用域的只有类,函数,和模块 for循环 if,else: 不能开辟自己的作用域 避免程序复用和重复调用,将这些写到一个.py文件中,做成一个模块,进行调 ...
- python基础 (序列化,os,sys,random,hashlib)
1.序列化 定义: JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.简单地说,JSON 可以将 JavaScript 对象中表示的一组数据转换为字符串,然 ...
- python基础之序列化 time random os
序列化与反序列化 json pickle 1.什么是序列化与反序列化? 序列化就是将内存中的数据结构转成一种中间格式储存到硬盘或者基于网络传输 反序列化是网络,硬盘将被序列化的对象重新读到内存 2. ...
- 18 python 初学(time、random 模块)
# _author: lily # _date: 2019/1/13 import time import datetime print(help(time)) # print(time.time() ...
- python学习笔记(七)- 递归、python内置函数、random模块
1.函数的不固定参数: #参数不是必填的.没有限制参数的个数.返回参数组的元组 def syz(*args): #参数组,不限制参数个数 #‘args’参数的名字可以随便命名 print(args) ...
- python(30)——【random模块】【if __name__ =='__main__'】【os模块】
一.random模块(随机模块) 1.random 常用模块介绍 import random print(random.random()) #返回[0,1)之间的随机浮点数 print(random. ...
- python标准库介绍——27 random 模块详解
==random 模块== "Anyone who considers arithmetical methods of producing random digits is, of cour ...
- Python基础笔记系列十:模块
本系列教程供个人学习笔记使用,如果您要浏览可能需要其它编程语言基础(如C语言),why?因为我写得烂啊,只有我自己看得懂!! 模块 #1.类比于java中的jar包,模块能让你能够有逻辑地组织你的Py ...
随机推荐
- OpenCvSharp尝试
OpenCvSharp是封装了OpenCV的.net版本 项目地址:https://github.com/shimat/opencvsharp 简单使用: 1.NuGet安装 2.使用OpenCvSh ...
- 高通 NXP NFC(PN547PN548) 移植流程 android6.0
一.驱动部分 首先向NXP 的 fae要android 6.0 bring up的代码,如:NFC_NCIHALx_AR0F.4.3.0_M_NoSE 结构目录如下: 1. 添加驱动文件 高通平台需使 ...
- Windows Server 2016-Hyper-V 2016新增功能
本文解释了Windows Server 2016和Microsoft Hyper-V Server 2016上Hyper-V的新增功能和变更功能. 与Connected Standby兼容(新) 在使 ...
- 新安装 Ubuntu 系统设置root用户密码!谨此纪念自己踩过的坑!
Ubuntu 在安装过程中创建的用户为普通用户,而root 用户密码该如何设置呢? 执行以下命令即可: sudo passwd root 提示你输入普通用户密码,然后设置root用户的密码!这样,就更 ...
- LeetCode算法题-Reverse Bits(Java实现)
这是悦乐书的第185次更新,第187篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第44题(顺位题号是190).给定32位无符号整数,求它的反转位.例如: 输入:4326 ...
- PHP防盗链的基本思想&&防盗链的设置方法
PHP防盗链的基本思想&&防盗链的设置方法 网站盗链会大量消耗被盗链网站的带宽,而真正的点击率也许会很小,严重损害了被盗链网站的利益.本文主要介绍用PHP实现防盗链的方法以及基本思想, ...
- 试验一下Golang 网络爬虫框架gocolly/colly
参考:http://www.cnblogs.com/majianguo/p/8186429.html 框架源码在 github.com/gocolly/colly 代码如下(github源码中的dem ...
- [matlab] 1.拟合
x = [1 2 3 4 5 6 7 8 9 ]; y = [9 7 6 3 -1 2 5 7 20]; p=polyfit(x,y,3); %数字代表拟合函数的阶数 xi=0:0.01:10; yi ...
- 转://Oracle Golden Gate 概念和原理
引言:Oracle Golden Gate是Oracle旗下一款支持异构平台之间高级复制技术,是Oracle力推一种HA高可用产品,简称“OGG”,可以实现Active-Active 双业务中心架构 ...
- ssh原理图解
SSH(Secure Shell)是一种能够以安全的方式提供远程登录的协议,也是目前远程管理Linux系统的首选方式.在此之前,远程登录一般常用FTP和Telnet,但是它们以明文的形式在网络中传输账 ...