Python常用标准库之datetime、random、hashlib、itertools

库:具有相关功能模块的集合

import sys.builtin_module_names  #返回内建模块的名字
modules 查看所有可用模块的名字

1.1.1获取当前日期和时间
from datetime import datetime
now = datetime.now()
now1 = datetime.today()
print(now)  #2018-11-05 16:59:02.950893
print(now1) #2018-11-05 16:59:02.950894

dt = now.timestamp()    #timestamp指1970-1-1 00:00:00到现在时间的秒数
print(dt)   #1541408474.822436

dt_1 = datetime.fromtimestamp(dt)
print(dt_1) #1541408474.822436
1.1.2 datetime与str
str转换成datetime:strptime()
datetime转换成str:strftime()
先格式化一个格式:('%Y-%m-%d %H:%M:%S')

from datetime import datetime
s = '2018-3-27 14:23:34'
print(type(s))
f = '%Y-%m-%d %H:%M:%S'
c = datetime.strptime(s,f)
print(c)        #2018-03-27 14:23:34

now = datetime.now()
print(now)  #2018-11-05 17:17:24.497495

print(now.strftime('%a-%b-%d %H:%M:%S'))    #Mon-Nov-05 17:16:12

1.1.3  calendar

import calendar
print(calendar.month(2018,4))

1.1.4 创建一个坐标

from collections import namedtuple
Point = namedtuple('Point', ['x', 'y'])
p = Point(1,2)
print(p.x,p.y)
print(isinstance(p,Point))  #True

from collections import namedtuple
Circle = namedtuple('Circle', ['x', 'y','r'])
p = Circle(1,2,3)
print(p.x,p.y,p.r)
1.1.5  deque  
为了高效实现插入和删除的双向列表(列表有序不唯一)
from collections import deque
q = deque(['a','b','c'])
q.append('d')
q.appendleft('z')
print(q)       #deque(['z', 'a', 'b', 'c', 'd'])
q.pop()
print(q)        #deque(['z', 'a', 'b', 'c'])
q.popleft()
print(q)        #deque(['a', 'b', 'c'])

 1.1.6  defaultdict

平时使用dict,如果引用的key不存在时,会抛出KeyError defaultdict
当key不存在时,返回一个默认值
from collections import defaultdict
dd = defaultdict(lambda :'N/A')
dd['key1'] = 'aaa'
print(dd['key1'])       #aaa
print(dd['key2'])       #N/A

 1.1.7 Counter  计数器,不存在返回0

from collections import Counter
c = Counter()     #0
print(c['hello'])
for ch in 'programming':
    c[ch] += 1
print(c)      #Counter({'r': 2, 'g': 2, 'm': 2, 'p': 1, 'o': 1, 'a': 1, 'i': 1, 'n': 1})

 1.1.8  random

from collections import Counter
c = Counter()     #0
print(c['hello'])
for ch in 'programming':
    c[ch] += 1
print(c)      #Counter({'r': 2, 'g': 2, 'm': 2, 'p': 1, 'o': 1, 'a': 1, 'i': 1, 'n': 1})

from collections import random
import random
print(random.randint(1,10))
print(random.random())

2.0  MD5算法---32位16进制数

import hashlib
md5 = hashlib.md5()
s = "hello python how to use md5in python".encode("utf-8")
md5.update(s)
result = md5.hexdigest()
print(result)   #ce1f253dc79dd372c57e0d295ab971e3

3.0  SHA1算法--40位16进制数

import hashlib
# md5 = hashlib.md5()
sha = hashlib.sha1()
s = "hello python how to use md5in python".encode("utf-8")
# md5.update(s)
sha.update(s)
result = sha.hexdigest()
print(result)       #e1d3e8e095660fca20e6ecaaddf6810648d8b8d9

4.0  itertools模块---用于迭代

无限迭代器
import itertools
cs = itertools.cycle('ABC')
for c in cs:
    print(c)    #输出无限迭代后的结果

groupby()将迭代器中相邻的重复元素挑出来放在一起
import itertools
for key,group in itertools.groupby('AAAbbbbCCCBBB'):
    print(key,list(group))
'''结果:
A ['A', 'A', 'A']
b ['b', 'b', 'b', 'b']
C ['C', 'C', 'C']
B ['B', 'B', 'B']
'''

 5.0 爬虫模块

urlib模块:提供一系列用于操作URL的功能
re模块:为高级字符串处理正则表达式工具
posted @ 2018-11-05 17:19  大脸猫12581  阅读(273)  评论(0编辑  收藏  举报