Python 常用模块之time&datetime 和random
本节大纲:
- 模块介绍
- time &datetime模块
- random
一、模块介绍:
模块,用一砣代码实现了某个功能的代码集合。
类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合。而对于一个复杂的功能来,可能需要多个函数才能完成
(函数又可以在不同的.py文件中),n个 .py 文件组成的代码集合就称为模块。
如:os 是系统相关的模块;file是文件操作相关的模块
模块分为三种:
①自定义模块
②内置标准模块(又称标准库)
③开源模块
二、time &datetime模块
1 import time
2 import datetime
3
4 print(time.clock()) #返回处理器时间,3.3开始已废弃
5 print(time.process_time()) #返回处理器时间,3.3开始已废弃
6 print(time.time()) #返回当前系统时间戳
7 print(time.ctime()) #输出Tue Jan 26 18:23:48 2016 ,当前系统时间
8 print(time.ctime(time.time()-86640)) #将时间戳转为字符串格式
9 print(time.gmtime(time.time()-86640)) #将时间戳转换成struct_time格式
10 print(time.localtime(time.time()-86640)) #将时间戳转换成struct_time格式,但返回 的本地时间
11 print(time.mktime(time.localtime())) #与time.localtime()功能相反,将struct_time格式转回成时间戳格式
12 #time.sleep(4) #sleep
13 print(time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime()) ) #将struct_time格式转成指定的字符串格式
14 print(time.strptime("2016-01-28","%Y-%m-%d") ) #将字符串格式转换成struct_time格式
15
16 #datetime module
17
18 print(datetime.date.today()) #输出格式 2016-01-26
19 print(datetime.date.fromtimestamp(time.time()-864400) ) #2016-01-16 将时间戳转成日期格式
20 current_time = datetime.datetime.now() #
21 print(current_time) #输出2016-01-26 19:04:30.335935
22 print(current_time.timetuple()) #返回struct_time格式
23
24 #datetime.replace([year[, month[, day[, hour[, minute[, second[, microsecond[, tzinfo]]]]]]]])
25 print(current_time.replace(2014,9,12)) #输出2014-09-12 19:06:24.074900,返回当前时间,但指定的值将被替换
26
27 str_to_date = datetime.datetime.strptime("21/11/06 16:30", "%d/%m/%y %H:%M") #将字符串转换成日期格式
28 new_date = datetime.datetime.now() + datetime.timedelta(days=10) #比现在加10天
29 new_date = datetime.datetime.now() + datetime.timedelta(days=-10) #比现在减10天
30 new_date = datetime.datetime.now() + datetime.timedelta(hours=-10) #比现在减10小时
31 new_date = datetime.datetime.now() + datetime.timedelta(seconds=120) #比现在+120s
32 print(new_date)
格式如下表格:
| 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模块
1、随机数
eg:
import random print random.random() print random.randint(1,2) print random.randrange(1,10)
2、生成随机验证码
eg:
import random
checkcode = ''
for i in range(4):
current = random.randrange(0,4)
if current != i:
temp = chr(random.randint(65,90))
else:
temp = random.randint(0,9)
checkcode += str(temp)
print checkcode
Python 常用模块之time&datetime 和random的更多相关文章
- python常用模块之time&datetime模块
python常用模块之time&datetime模块 在平常的代码中,我们经常要与时间打交道.在python中,与时间处理有关的模块就包括:time和datetime,下面分别来介绍: 在开始 ...
- Python——常用模块(time/datetime, random, os, shutil, json/pickcle, collections, hashlib/hmac, contextlib)
1.time/datetime 这两个模块是与时间相关的模块,Python中通常用三种方式表示时间: #时间戳(timestamp):表示的是从1970年1月1日00:00:00开始按秒计算的偏移量. ...
- Python常用模块(logging&re&时间&random&os&sys&shutil&序列化&configparser&&hashlib)
一. logging(日志模块) 二 .re模块 三. 时间模块 四. random模块 五. os模块 六. sys模块 七. shutil模块 八. 序列化模块(json&pickle&a ...
- Python常用标准库之datetime、random、hashlib、itertools
库:具有相关功能模块的集合 import sys.builtin_module_names #返回内建模块的名字modules 查看所有可用模块的名字 1.1.1获取当前日期和时间 from date ...
- Python 常用模块系列学习(1)--random模块常用function总结--简单应用--验证码生成
random模块--random是一个生成器 首先: import random #导入模块 print (help(random)) #打印random模块帮助信息 常用function ...
- python常用模块---collections、time、random、os、sys、序列号模块
collections模块 在内置数据类型(dict.list.set.tuple)的基础上,collections模块还提供了几个额外的数据类型:Counter.deque.defaultdict. ...
- python 常用模块之random,os,sys 模块
python 常用模块random,os,sys 模块 python全栈开发OS模块,Random模块,sys模块 OS模块 os模块是与操作系统交互的一个接口,常见的函数以及用法见一下代码: #OS ...
- Python常用模块-随机数模块(random)
Python常用模块-随机数模块(random) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.常用方法举例 #!/usr/bin/env python #_*_coding: ...
- Python常用模块-时间模块(time&datetime)
Python常用模块-时间模块(time & datetime) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.初始time模块 #!/usr/bin/env pyth ...
随机推荐
- 使用phpize安装php扩展
环境: CentOs 6.3 php 7 nginx 举例: 安装ssh2扩展 1.登陆http://pecl.php.net,搜索ssh2,如下图所示,注意版本的选择要根据php的版本来 2.下载s ...
- 用ThreadLocal类实现线程安全的正确姿势
大家通常知道,ThreadLocal类可以帮助我们实现线程的安全性,这个类能使线程中的某个值与保存值的对象关联起来.ThreadLocal提供了get与set等访问接口或方法,这些方法为每个使用该变量 ...
- Nancy之Pipelines三兄弟(Before After OnError)
一.简单描述 Before:如果返回null,拦截器将主动权转给路由:如果返回Response对象,则路由不起作用. After : 没有返回值,可以在这里修改或替换当前的Response. OnEr ...
- 那些年用过的xUnit.net的简单用法
0x01 前言 单元测试,总是伴随着我们的开发过程,优劣自行google.当然呢,不排除有些公司是不做单元测试的, 但是呢,学多一点东西用来防身还是可以的. 0x02 简单的Demo 写个两数求和的方 ...
- 基于C#的MongoDB数据库开发应用(4)--Redis的安装及使用
在前面介绍了三篇关于MongoDB数据库的开发使用文章,严格来讲这个不能归类于MongoDB数据库开发,不过Redis又有着和MongoDB数据库非常密切的关系,它们两者很接近,Redis主要是内存中 ...
- jQuery仿阿里云购买选择购买时间长度
效果:http://hovertree.com/texiao/jquery/61/ jQuery仿阿里云购买服务器选择时间长度,操作简单,只需点击所要选的时间段 代码: <!doctype ht ...
- linq lambda left join
//var list = table1.Join(table2, ee => ee.Id, ff => ff.table1_Id, (ee, ff) => new { ee, ff ...
- c中使用gets() 提示warning: this program uses gets(), which is unsafe.
今天在C代码中使用gets()时提示“warning: this program uses gets(), which is unsafe.”,然后这个程序还能运行,无聊的我开始查阅资料,为啥gets ...
- Web测试介绍一 UI测试
随着Web 2.0技术的迅速发展,许多公司都开发了一些基于Web的网站服务,通常在设计开发Web应用系统的时候很难模拟出大量用户同时访问系统的实际情况,因此,当Web网站遇到访问高峰时, ...
- [翻译]Java HashMap工作原理
大部分Java开发者都在使用Map,特别是HashMap.HashMap是一种简单但强大的方式去存储和获取数据.但有多少开发者知道HashMap内部如何工作呢?几天前,我阅读了java.util.Ha ...