第三方模块

一 time与datetime模块

   时间戳(timestamp):通常来说,时间戳表示的是当前时间减去1970年1月1日00:00:00的秒数。

import time
print(time.time()) #时间戳:1533525058.7580311
通常可以用于程序运行开始时time.time()一次,程序运行结束再time.time一次,然后后者减去前者,用于统计程序运行的时间。


格式化的时间字符串:
print(time.strftime('%Y-%m-%d %X'))  #格式化的字符串时间:2018-08-06 11:22:41
print(time.strftime('%Y-%m-%d %H:%M:%S %p'))  #格式化的字符串时间:2018-08-06 11:41:22 AM


格式化字符串时间格式:
%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.

 

结构化的时间(struct_time):struct_time元组共有9个元素共九个元素:(年,月,日,时,分,秒,一年中第几周,一年中第几天,夏令时)

print(time.localtime()) #本地时区的struct_time  time.struct_time(tm_year=2018, tm_mon=8, tm_mday=6, tm_hour=11, tm_min=34, tm_sec=31, tm_wday=0, tm_yday=218, tm_isdst=0)
obj=time.localtime()
print(obj.tm_year,obj.tm_mon) #可以以此取到你想要的值,比如该例中的年、月:2018 8
print(time.gmtime())    #UTC时区的struct_time   time.struct_time(tm_year=2018, tm_mon=8, tm_mday=6, tm_hour=3, tm_min=34, tm_sec=31, tm_wday=0, tm_yday=218, tm_isdst=0)


时间转换:


#timestamp<----->struct_time
struct_time=time.localtime(3131313)
timestamp=time.mktime(struct_time)
print(timestamp)
print(struct_time)

# struct_time<-------->format string
struct_time=time.strptime('2017:03-01 11:11:11','%Y:%m-%d %H:%M:%S')
print(struct_time)

print(time.strftime('%Y-%m-%d',struct_time))

import datetime                               #导入datetime模块
print(time.strftime('%Y-%m-%d %X'))
print(datetime.datetime.now()) #2018-08-06 14:31:45.772653

print(datetime.date.fromtimestamp(1341313)) #将时间戳格式的时间格式化为1970-01-16时间格式

print(datetime.datetime.fromtimestamp(1341313)) #将时间戳格式的时间格式化为1970-01-16 20:35:13时间格式

print(datetime.datetime.now() + datetime.timedelta(days=3))
print(datetime.datetime.now() + datetime.timedelta(days=-3))
print(datetime.datetime.now() - datetime.timedelta(days=3))

obj=datetime.datetime.now()
print(obj.replace(year=2017,month=8)) #替换时间

二 random模块

import random
  
print(random.random())#(0,1)----float    大于0且小于1之间的小数
  
print(random.randint(1,3))  #[1,3]    大于等于1且小于等于3之间的整数
 
print(random.randrange(1,3)) #[1,3)    大于等于1且小于3之间的整数
 
print(random.choice([1,'23',[4,5]]))    #1或者23或者[4,5]
  
print(random.sample([1,'23',[4,5]],2))  #列表元素任意2个组合
  
print(random.uniform(1,3))              #大于1小于3的小数,如1.927109612082716 
 

item=[1,3,5,7,9]
random.shuffle(item) #打乱item的顺序,相当于"洗牌"
print(item)

posted on 2018-08-06 15:43  昵称不能重复  阅读(140)  评论(0)    收藏  举报

导航