Python基础之十二常用內建模块

来源:互联网 发布:淘宝洗衣液假货多吗 编辑:程序博客网 时间:2024/06/11 11:59
'''    datetime        datetime是Python处理日期和时间的标准库'''###########################获取当前日期和时间from datetime import datetimenow = datetime.now()#获取当前datetimeprint(now)print(type(now))###########################获取指定日期和时间dt = datetime(2015, 4, 19,12, 30)#用指定日期穿件datetimeprint(dt)###########################datetime转换为timestampprint(dt.timestamp())#把datetime转换为timestamp###########################timestamp转换为datetimet = 12343534.0print(datetime.fromtimestamp(t))print(datetime.utcfromtimestamp(t))###########################str转换为datetimecday = datetime.strptime('2015-6-1 18:19:59', '%Y-%m-%d %H:%M:%S')print(cday)###########################datetime转换为strnow = datetime.now()print(now.strftime('%a, %b %d %H:%M'))'''    collections        collections是Python内建一个集合模块,提供了许多有用的集合类'''###########################namedtuplefrom collections import namedtuplePoint = namedtuple('Point', ['x', 'y'])p = Point(1,2)print(p.x)print(p.y)###########################dequefrom  collections import dequeq = deque(['a', 'b', 'c'])q.append('x')q.append('y')print(q)###########################defaultdict返回默认值from collections import defaultdictdd = defaultdict(lambda : 'N/A')dd['key1'] = 'abc'print(dd['key1'])print(dd['key2'])###########################OrderdDict保证dict顺序from collections import OrderedDictd = dict([('a', 1), ('b', 2), ('c', 3)])print(d)od = OrderedDict([('a', 1), ('b', 2), ('c', 3)])print(od)###########################Counter简单计数器from collections import Counterc = Counter()for ch in 'programming':    c[ch] = c[ch] +1print(c)'''    urllib        urllib提供一系列用于操作URL的功能'''###########################Getfrom urllib import requestwith request.urlopen("https://api.douban.com/v2/book/2129650") as f:    data = f.read()    print('Status:', f.status, f.reason)    for k, v in f.getheaders():        print('%s: %s' % (k, v))    print('Data:', data.decode('utf-8'))

更多精彩内容访问个人站点www.gaocaishun.cn

0 0