Python 常用模块之time&datetime 和random

时间:2021-11-08 15:12:25

本节大纲:

  1. 模块介绍
  2. time &datetime模块
  3. 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