进阶
日期与时间
import datetime
import time # 引入time模块
import calendar
#获取当前日期时间
now=datetime.datetime.now()
print(now)
#获取指定日期
d=datetime.datetime(2019,10,1,12,23,40)
print(d)
#日期转字符串
d2=d.strftime("%Y/%m/%d %H:%M:%S")
print(d2)
#字符串转日期
s="2020-8-15 2:30:20"
d3=datetime.datetime.strptime(s,"%Y-%m-%d %H:%M:%S")
print(d3)
#获取本地时间
ticks = time.time()
print ("当前时间戳为:", ticks)
localtime = time.localtime(time.time())
print ("本地时间为 :", localtime)
#格式化
localtime = time.asctime( time.localtime(time.time()) )
print ("本地时间为 :", localtime)
# 格式化成2016-03-20 11:45:39形式
print (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
# 格式化成Sat Mar 28 22:24:24 2016形式
print (time.strftime("%a %b %d %H:%M:%S %Y", time.localtime()))
# 将格式字符串转换为时间戳
a = "Sat Mar 28 22:24:24 2016"
print (time.mktime(time.strptime(a,"%a %b %d %H:%M:%S %Y")))
cal = calendar.month(2016, 1)
print ("以下输出2016年1月份的日历:")
print (cal)
CGI编程
CGI(Common Gateway Interface),通用网关接口,它是一段程序,运行在服务器上如:HTTP服务器,提供同客户端HTML页面的接口。
JSON
一种数据结构,方便数据传递,类似python中的字典
#json,具有特定结构的字符串
import json
#json转字典
j='{"city":"北京","天气":"晴"}'
p=json.loads(j)
print(p)
#字典转json
d={"city":"北京","天气":"晴"}
j2=json.dumps(d,ensure_ascii=False)
print(j2)
XML解析
XML 指可扩展标记语言(EXtensible Markup Language),是一种很像HTML的标记语言,XML 的设计宗旨是传输数据,而不是显示数据
正则表达式
正则表达式是一个特殊的字符序列,它能帮助你方便的检查一个字符串是否与某种模式匹配。
MySql
使用mysql.connector或pymysql进行连接
编码和解码
encode() 编码
decode() 解码
#编码
str1="下班啦,兄die"
str2=str1.encode("utf-8")
print(str2)
#解码
str3=str2.decode("utf-8")
print(str3)
来源:oschina
链接:https://my.oschina.net/u/4412764/blog/3236486
