【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.時期和時間sql

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)
相關文章
相關標籤/搜索