# 从URL检索数据 from urllib.request import urlopen with urlopen('http://worldtimeapi.org/api/timezone/etc/UTC.txt') as response: for line in response: line = line.decode() # Convert bytes to a str if line.startswith('datetime'): print(line.rstrip()) # Remove trailing newline
# 发送邮件 import smtplib server = smtplib.SMTP('localhost') server.sendmail('soothsayer@example.org', 'jcaesar@example.org', """To: jcaesar@example.org From: soothsayer@example.org Beware the Ides of March. """) server.quit()
日期&时间
1 2 3 4 5 6 7 8 9 10
from datetime import date
now = date.today() now now.strftime("%m-%d-%y. %d %b %Y is a %A on the %d day of %B.")
# dates support calendar arithmetic birthday = date(1964, 7, 31) age = now - birthday age.days
数据压缩
常用模块:zlibgzipbz2lzmazipfiletarfile 。
1 2 3 4 5 6 7 8
import zlib
s = b'witch which has which witches wrist watch' len(s) # 41 t = zlib.compress(s) len(t) # 37 zlib.decompress(t) zlib.crc32(s)