1. 程式人生 > >【python】基礎入門

【python】基礎入門

1.正則表示式

import re
sql="aaa$1bbbbccccc$2sdfsd gps_install_note_id =$3;"

regexp=r'\$\d+'
# 編譯正則表示式
pattern=re.compile(regexp,re.M)
# 從開始位置查詢
match = pattern.match(sql)
print(match)
## 查詢所有匹配項,返回結果為列表:['$1', '$2', '$3']
m2 = pattern.findall(sql)
print(m2)
# 查詢,返回第一個匹配
matchObj=re.search(regexp,sql,re.M);
print(matchObj.group()) ## 查詢所有匹配項,返回一個迭代器 iterator = re.finditer(regexp, sql, re.M) print(iterator) for m in iterator: print(m.group()) ## 正則替換 將匹配項替換為 *** sub = re.sub(regexp, "***", sql, re.M) print(sub) ## 正則替換,將匹配項 首尾加上下劃線 def do(matcher): return '_'+matcher.group()+'_' re_sub = re.sub(regexp, do, sql, re.M)
print(re_sub) split = re.split(regexp, sql) for line in split: print(line)

2.時期和時間

import time
import calendar
##當前時間戳
timestamp = time.time()
print(timestamp)
#返回元組型別日期
localtime = time.localtime(timestamp)
print(localtime)
#格式化的日期
asctime = time.asctime(localtime)
print(asctime)
ftm='%Y-%m-%d %H:%M:%S
' #根據元組日期格式化日期 time_fmt_str = time.strftime(ftm, localtime) print(time_fmt_str) #轉換字串為元組型別日期 str_to_time = time.strptime(time_fmt_str, ftm) print(str_to_time) #列印當前2018年11月的日曆 month = calendar.month(2018, 11) print(month)