python 入坑路-模块分类

python 模块分类

1.标准库

2.开源模块(第三方模块)

3.自定义模块

1.标准库(os,sys 等python内置的模块)time,datetime

 

时间:3个格式  a. "2018-1-4 10:56:00"  b.时间戳  c。struct_time 以元组的表现形式 9个元素

In [5]: time.localtime()  #struct_time
Out[5]: time.struct_time(tm_year=2018, tm_mon=1, tm_mday=4, tm_hour=11, tm_min=1
, tm_sec=37, tm_wday=3, tm_yday=4, tm_isdst=0)

 9个元素:年、月、日、时、分、秒、星期、年的第几天、是否夏令时 0:不是

import time,datetime

time.time()
1515034690.6331031  #换算 从1970年1月1日,by the way: unix 诞生元年,纪念

 time.timezone  -28800 :格林威治时间--本时区 的差 8小时

time.time() 获取时间戳。

 time.time()
 1515035514.5982313

time.sleep()  睡几秒

time.sleep(2)

time.gmtime()

import time

time.gmtime() 

#获取 当前格林威治时间, 如果传入  秒,转换为格林威治时间
In [13]: time.gmtime(1000000000)
Out[13]: time.struct_time(tm_year=2001, tm_mon=9, tm_mday=9, tm_hour=1, tm_min=4
6, tm_sec=40, tm_wday=6, tm_yday=252, tm_isdst=0)

 time.localtime()

In [14]: time.localtime(10000000)
Out[14]: time.struct_time(tm_year=1970, tm_mon=4, tm_mday=27, tm_hour=1, tm_min=
46, tm_sec=40, tm_wday=0, tm_yday=117, tm_isdst=0)

 在 time.localtime() 获取各元素

import time

x=time.localtime()

dir(x)


In [21]: x
Out[21]: time.struct_time(tm_year=2018, tm_mon=1, tm_mday=4, tm_hour=11, tm_min=
19, tm_sec=45, tm_wday=3, tm_yday=4, tm_isdst=0)

In [22]: x.tm_year  #获取年
Out[22]: 2018

In [23]: x.tm_mon#获取月份
Out[23]: 1

In [24]: x.tm_mday#获取日期
Out[24]: 4

In [25]: x.tm_hour#获取小时
Out[25]: 11

In [26]: x.tm_min#获取分钟
Out[26]: 19

In [27]: x.tm_sec#获取秒
Out[27]: 45

In [28]: x.tm_wday#获取星期,从0开始,星期一 0, 星期四  3
Out[28]: 3

In [29]: x.tm_yday#获取一年第几天 
Out[29]: 4

In [30]: x.tm_zone#获取时区
Out[30]: '?D1¨²¡À¨º¡Á?¨º¡À??'

In [31]: x.tm_isdst #是否夏令时
Out[31]: 0

 

In [35]: x
Out[35]: time.struct_time(tm_year=2018, tm_mon=1, tm_mday=4, tm_hour=11, tm_min=
26, tm_sec=55, tm_wday=3, tm_yday=4, tm_isdst=0)

In [36]: time.mktime(x)
Out[36]: 1515036415.0

 

strftime(...)
    strftime(format[, tuple]) -> string

    Convert a time tuple to a string according to a format specification.
    See the library reference manual for formatting codes. When the time tuple
    is not present, current time as returned by localtime() is used.

    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.

    Other codes may be available on your platform.  See documentation for
    the C library strftime function.

 time.strftime("%Y-%m-%d %H:%M:%S",x)
 '2018-01-04 11:26:55'

 

In [59]: a=time.localtime()

In [60]: a
Out[60]: time.struct_time(tm_year=2018, tm_mon=1, tm_mday=4, tm_hour=11, tm_min=
47, tm_sec=5, tm_wday=3, tm_yday=4, tm_isdst=0)
 b=time.strftime("%Y-%m-%d %H:%M:%S",a)

 b
 '2018-01-04 11:47:05'

In [68]: c=time.strptime(b,"%Y-%m-%d %H:%M:%S")

In [69]: c
Out[69]: time.struct_time(tm_year=2018, tm_mon=1, tm_mday=4, tm_hour=11, tm_min=
47, tm_sec=5, tm_wday=3, tm_yday=4, tm_isdst=-1)

 

三个格式的转换:

时间戳转换为 struct_time (tuple)   time.gmtime() 转换为格林威治时间, time.localtime() 本地时间

本地时间(tuple) 转换为 时间戳 time.mktime

 

struct_time ----> 字符串  :time.strftime("格式",x)  ,格式可以自定义,按照函数的关键字

 

字符串---> struct_time  :  time.strptime(x,"格式")    格式需要跟给的字符串 一一对应

 

时间戳----> 字符串 :  time.ctiem()

元组---》 字符串 :timeasctime()

a=time.localtime(100003432)

In [111]: a
Out[111]: time.struct_time(tm_year=1973, tm_mon=3, tm_mday=3, tm_hour=18, tm_min
=43, tm_sec=52, tm_wday=5, tm_yday=62, tm_isdst=0)

time.asctime(a)
'Sat Mar  3 18:43:52 1973'

 

 time.ctime(100000232)
 'Sat Mar  3 17:50:32 1973'

 

datetime 模块

居于time 模块的高级封装

import datetime

datetime.datetime.now()  #获取当前时间

datetime.datetime.now()+datetime.timedelta(3) #获取3天后时间

datetime.datetime.now()+datetime.timedelta(-3)#获取3天前时间

datetime.datetime.now()+datetime.timedelta(hours=-3) #获取3小时前的时间,

#minutes ,seconds 。。。。

#直接修改时间

c_time=datetime.datetime.now()

c_time.replace(minute=3,hours=-2)

: c_time
: datetime.datetime(2018, 1, 4, 13, 16, 18, 621148)

: c_time.replace(hour=14)
: datetime.datetime(2018, 1, 4, 14, 16, 18, 621148)

]: c_time.replace(hour=14,minute=20)
]: datetime.datetime(2018, 1, 4, 14, 20, 18, 621148)

]: c_time.replace(hour=14,minute=20,year=2017)
]: datetime.datetime(2017, 1, 4, 14, 20, 18, 621148)

 random 模块

获取一个随机数,默认是 0-1

import random

random.random()
0.5446389288621797

random.random()
0.43145577548142977  #在 0-1之间取一个浮点数

 random.randint(0,3)
 1

 random.randint(0,3)
 0

 random.randint(0,3)
 3

 random.randint(0,3) # 在0-3 取一个整数 ,注意:包含3
 2

 random.randrange(0,3)
 1

 random.randrange(0,3)
 0

 random.randrange(0,3)  #在0-2随机取一个数,不包含3,跟range() 特性一样,区别于randint
 2

 random.choice("python")
 'n'

 random.choice("python")
 'y'

 random.choice("python") #从 pthon 中 选取一个字母

 random.choices([1,2,3,4],[10,20,30,5],k=3)
 [2, 2, 2]

 random.choices([1,2,3,4],[10,20,30,5],k=3)
 [2, 3, 1]
#从1,2,3,4  随机选取3个数,  权重,10,20,30,5 , 权重越大,概率越大。

 

random.uniform(1,10)
1.8204265898471998


#从1,10 随机取一个浮点数

 

random.sample('abcdefghijklmn',3)
['a', 'g', 'b']

random.sample('abcdefghijklmn',3)
['a', 'b', 'c']

#从字符串中随机取3个 

 

l=[]
for i in range(10)
    l.appen(i)

random.shuffle(l)

#把l 列表,洗牌,打乱顺序

 

生成验证码:

import random

checkcode=""

for i in range(4):
    current = random.randint(0, 4)
    if i==current:
        tmp=chr(random.randint(65,90))
    else:
        tmp= random.randint(0,9)

    checkcode +=str(tmp)
print(checkcode)

 

posted @ 2018-01-04 14:39  东郭仔  阅读(183)  评论(0)    收藏  举报