人山人海python
看獲得的看不到的都應該看到程序員
1.查看一下2000000000時間戳時間表示的年月日正則表達式
2.將2008-8-8轉換成時間戳時間算法
3.請將當前時間的當前月1號的時間戳時間取出來 - 函數運維
4.計算時間差dom
2018-8-19 22:10:8 2018-8-20 11:07:3函數
通過了多少時分秒spa
方法操作系統
學練改管測命令行
代碼質量相關的參數: 簡潔渡\時間\空間
複習正則 難中之難
正則表達式
元字符:\w\d\s\n\t\W\D\S\b是以什麼結尾 ^匹配一個字符串的開始
$匹配一個字符串的結尾[] ()|[^]除了字符組中的全部字符
[0-9]\d 這種狀況下 應該\d
[1-9] 特殊狀況 特殊處理
[\da-zA-Z]
量次:{n}{n,}{n,m} * ? +
轉義符 :
Python str: 「\」
正則表達式中的」\」到了python中都會變成」\\」
r」\w」 在python當中\不轉義了,在python中就是一個普通的」\」,
可是在正則表達式中他仍是表示一個轉義符
貪婪匹配/惰性匹配:
.*?貪婪匹配 匹配任意字符內容最屢次,知道最後一個x中止 回溯算法
.*?x 惰性 匹配任意內容最少次,直到遇到第一個x就中止
Re模塊
Findall 匹配全部 返回一個列表裝上所匹配到的內容
Search 匹配第一個 返回的是個變量,須要經過.group()接受返回的東西,
若是沒匹配到就返回None
Match至關於在search前面加了個^只匹配特定的字符
Split 切割 返回的是一個列表 加個括號能夠把被切掉的內容放到列表中
Sub subn 都是替換re.sub(「被替換的內容」,替換的內容,字符串,數字表示替換的次數)
區別在乎 subn 返回的是一個元祖(替換後的內容,實際被替換的次數)
Finditer complie
Finditer 返回一個迭代器,全部匹配到的內容須要迭代取到,
迭代取到的每個結果 都須要group 取具體值 效果:能夠節省內存
Complie 編譯先把一個正則表達式編譯,編譯以後,在以後屢次使用的過程當中不用從新編譯
== 節省時間 提升效率
分組
1,給不止一個字符的總體量詞約束的時候www(\.\w+)+ www.baidu.com
2,優先顯示,當要匹配的內容和不想匹配的內容混在一塊兒的時候
就匹配出全部內容,可是對實際須要的內容進行分組
3,分組和re模塊中的方法:
Findall :分組優先顯示 取消分組優先(?:正則表達式)
Search :
能夠經過.group(index)來取分組中的內容
能夠經過.group(name)來取分組中的內容
正則(?P<name>正則])
使用這個分組 ?P=name
Split :會保留分組內的內容到切割的結果中
Import random 取隨機數的模塊
取隨機小數 : 數學計算
# print(random.random()) #取0-1之間的小數
# print(random.uniform(0,8))#取任意倆個數之間的小數
#
#
# print(random.randint(1,3))#左右都是閉區間,能夠取到最邊界的值
# print(random.randrange(1,3))#這是左閉合又開區間,能夠取到左側,取不到右側,
# #取隨機整數,彩票,抽獎
# l=[ "a","b",(1,2),2333]
# print(random.choice(l))# 隨機從l中抽取一個返回
# #b
# print(random.sample(l,3))#解釋爲抽樣分析 前面是須要須要抽取的可迭代對象,後面是要抽取的個數,
# ['b', 2333, 'a']
Random.shuffle(list)
l=[ "a","b",(1,2),2333]
random.shuffle(l)
print(l)
[(1, 2), 'a', 2333, 'b
驗證碼_ 課上練習
4位數字驗證碼
6位數字驗證碼
7位數字+字母驗證碼
import random
s=""
for c in range(4):
num=random.randint(0,9)
s +=str(num)
print(s)
1142
def yzm(n = 6):
s=""
for c in range(n):
num = random.randint(0, 9)
s += str(num)
return s
get=yzm()
print(get)
print(yzm(8))
073245
10012804
升級版本
def yzm(n=6,alphanet=True):#alphamet位False 則顯示結果爲數字
s=""
for c in range(n):
number = str(random.randint(0, 9))
if alphanet:
alphanet_upper = chr(random.randint(65, 90)) #ascii 65是大寫 97是小寫
alphanet_lower = chr(random.randint(97, 122))
number = random.choice([number, alphanet_lower, alphanet_upper])
s += number
return s
print(yzm(4,alphanet=False))
print(yzm())
時間模塊
Import time
Time.sleep(2) 程序走到這裏暫停2秒
Time 模塊 主要是和時間打交道的
時間合適
‘’2018.-8-20」 ‘’2018.8.20’ 字符串數據類型 格式化時間_給人看的
15347344655.617272 浮點型數據類型,以s秒爲單位 時間戳時間-給機器看的
1970 1 1 0:0:0 英國倫敦時間 爲起始的位置
在中國 默認是北京時間 比倫敦早八個小時
時間戳時間
Print(time.time)
print(time.time())#時間戳時間
print(time.strftime("%Y-%m-%d %H:%M:%S"))#當前爲北京時間格式化時間
print(time.strftime("%c"))#"%c" 默認爲英國格式 星期 月份 日 時:分:秒 年
#結構化時間
strct_time=time.localtime()
print(strct_time)
print(strct_time.tm_mon)#這個想輸出什麼就標記tm_?
1534759016.476882
2018-08-20 17:56:56
Mon Aug 20 17:56:56 2018
time.struct_time(tm_year=2018, tm_mon=8, tm_mday=20, tm_hour=17, tm_min=56, tm_sec=56, tm_wday=0, tm_yday=232, tm_isdst=0)
8
時間戳時間轉化爲格式化時間
# struct_time = time.localtime()
# print(struct_time)
# format_time = time.strftime(struct_time)
# print(format_time)
# struct_time = time.localtime(1500000000)
# print(time.strftime("%Y-%m-%d %H:%M:%S",struct_time))
#2017-07-14 10:40:00
#格式化時間轉時間戳
# struct_time=time.strptime("2018-8-8","%Y-%m-%d")
# print(struct_time)
# res=time.mktime(struct_time)
# print(res)
#
# # 3請把當前時間的當前月 1號的時間戳時間取出來-
# def get():
# struct_time=time.localtime()
# struct_time2=time.strptime("%s-%s-1"%(struct_time.tm_year,struct_time.tm_mon),'%Y-%m-%d')
# return time.mktime(struct_time2)
# print(get())
# 計算時間差 _函數
str_time1="2018-8-19 22:10:8"
str_time2="2018-8-29 11:7:8"
struct_time1=time.strptime(str_time1,'%Y-%m-%d %H:%M:%S')
struct_time2=time.strptime(str_time2,'%Y-%m-%d %H:%M:%S')
timestamp1 = time.mktime(struct_time1)
timestamp2 = time.mktime(struct_time2)
sub_time = timestamp2-timestamp1
gm_time = time.gmtime(sub_time)
print('過去了%d年%d月%d天%d小時%d分鐘%d秒'%(gm_time.tm_year-1970,gm_time.tm_mon-1,
gm_time.tm_mday-1,gm_time.tm_hour,
gm_time.tm_min,gm_time.tm_sec))
過去了0年0月9天12小時57分鐘0秒
\
\
Sys是和python 解釋器打交道的
Sys.argv
Print(sys.argv) # argv 的第一個參數 是python這個命令後面的值
usr = input('username')
# pwd = input('password')
# usr = sys.argv[1]
# pwd = sys.argv[2]
# if usr == 'alex' and pwd == 'alex3714':
# print('登陸成功')
# else:
# exit()
1. 程序員 運維人員 在命令行運行代碼
# 2. 操做系統input事件 阻塞 退出了CPU的競爭
# sys.path
# print(sys.path)
# 模塊是存在解釋器裏的麼??? 不是
# 模塊應該是存在硬盤上
# 可是我在使用的時候 import --> 這個模塊纔到內存中
# 一個模塊可否被順利的導入 全看sys.path下面有沒有這個模塊所在的
# 自定義模塊的時候 導入模塊的時候 還須要再關注 sys.path
import re
# sys.modules
# print(sys.modules) # 是咱們導入到內存中的全部模塊的名字 : 這個模塊的內存地址
# print(sys.modules['re'].findall('\d','abc126'))
Os 模塊
Os 是和操做系統交互的模塊
import os# os.makedirs("dir1/dir2")# os.removedirs("dir1/dir2")# print(os.stat(r'D:\zzy\8.15\998.py'))#exec/eval 執行的是字符串數據類型的python代碼# os.system("dir")## ret=os.popen("dir")# s=ret.read()# print(s)# os.listdir / os.path.join# file_lst = os.listdir('D:\zzy\8.15')# for path in file_lst:# print(os.path.join('D:\zzy\8.15',path))### print(">>>>",os.getcwd())# ret=os.popen("dir")# s=ret.read()# print(s)os.chdir('D:\zzy\8.15') # 切換當前的工做目錄ret = os.popen('dir') # 是和作查看類的操做s =ret.read()print(s)