1. 程式人生 > 程式設計 >SpringBoot 如何編寫配置檔案

SpringBoot 如何編寫配置檔案

閱讀目錄

一 time與datetime模組

在Python中,通常有這幾種方式來表示時間:

  • 時間戳(timestamp):通常來說,時間戳表示的是從1970年1月1日00:00:00開始按秒計算的偏移量。我們執行“type(time.time())”,返回的是float型別。
  • 格式化的時間字串(Format String)
  • 結構化的時間(struct_time):struct_time元組共有9個元素共九個元素:(年,月,日,時,分,秒,一年中第幾周,一年中第幾天,夏令時)
1 import time
2 #--------------------------我們先以當前時間為準,讓大家快速認識三種形式的時間
3 print(time.time()) # 時間戳:1487130156.419527
4 print(time.strftime("%Y-%m-%d %X")) #格式化的時間字串:'2017-02-15 11:40:53'
5 
6 print(time.localtime()) #本地時區的struct_time
7 print(time.gmtime()) #UTC時區的struct_time
%a    Locale’s abbreviated weekday name.     
%A    Locale’s full weekday name.     
%b    Locale’s abbreviated month name.     
%B    Locale’s full month name.     
%c    Locale’s appropriate date and time representation.     
%d    Day of the month as a decimal number [01,31].     
%H Hour (24-hour clock) as a decimal number [00,23]. %I Hour (12-hour clock) as a decimal number [01,12]. %j Day of the year as a decimal number [001,366]. %m Month as a decimal number [01,12]. %M Minute as a decimal number [00,59]. %p Locale’s equivalent of either AM or PM. (1) %S Second as a decimal number [00,61]. (2) %U Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0. (3) %w Weekday as a decimal number [0(Sunday),6]. %W Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0. (3) %x Locale’s appropriate date representation. %X Locale’s appropriate time representation. %y Year without century as a decimal number [00,99]. %Y Year with century as a decimal number. %z Time zone offset indicating a positive or negative time difference from UTC/GMT of the form +HHMM or -HHMM, where H represents decimal hour digits and M represents decimal minute digits [-23:59, +23:59]. %Z Time zone name (no characters if no time zone exists). %% A literal '%' character.
格式化字串的時間格式

其中計算機認識的時間只能是'時間戳'格式,而程式設計師可處理的或者說人類能看懂的時間有: '格式化的時間字串','結構化的時間',於是有了下圖的轉換關係

 1 #--------------------------按圖1轉換時間
 2 # localtime([secs])
 3 # 將一個時間戳轉換為當前時區的struct_time。secs引數未提供,則以當前時間為準。
 4 time.localtime()
 5 time.localtime(1473525444.037215)
 6 
 7 # gmtime([secs]) 和localtime()方法類似,gmtime()方法是將一個時間戳轉換為UTC時區(0時區)的struct_time。
 8 
 9 # mktime(t) : 將一個struct_time轉化為時間戳。
10 print(time.mktime(time.localtime()))#1473525749.0
11 
12 
13 # strftime(format[, t]) : 把一個代表時間的元組或者struct_time(如由time.localtime()和
14 # time.gmtime()返回)轉化為格式化的時間字串。如果t未指定,將傳入time.localtime()。如果元組中任何一個
15 # 元素越界,ValueError的錯誤將會被丟擲。
16 print(time.strftime("%Y-%m-%d %X", time.localtime()))#2016-09-11 00:49:56
17 
18 # time.strptime(string[, format])
19 # 把一個格式化時間字串轉化為struct_time。實際上它和strftime()是逆操作。
20 print(time.strptime('2011-05-05 16:37:06', '%Y-%m-%d %X'))
21 #time.struct_time(tm_year=2011, tm_mon=5, tm_mday=5, tm_hour=16, tm_min=37, tm_sec=6,
22 #  tm_wday=3, tm_yday=125, tm_isdst=-1)
23 #在這個函式中,format預設為:"%a %b %d %H:%M:%S %Y"。

1 #--------------------------按圖2轉換時間
2 # asctime([t]) : 把一個表示時間的元組或者struct_time表示為這種形式:'Sun Jun 20 23:21:05 1993'。
3 # 如果沒有引數,將會將time.localtime()作為引數傳入。
4 print(time.asctime())#Sun Sep 11 00:43:43 2016
5 
6 # ctime([secs]) : 把一個時間戳(按秒計算的浮點數)轉化為time.asctime()的形式。如果引數未給或者為
7 # None的時候,將會預設time.time()為引數。它的作用相當於time.asctime(time.localtime(secs))。
8 print(time.ctime())  # Sun Sep 11 00:46:38 2016
9 print(time.ctime(time.time()))  # Sun Sep 11 00:46:38 2016
1 #--------------------------其他用法
2 # sleep(secs)
3 # 執行緒推遲指定的時間執行,單位為秒。
#時間加減
import datetime

# print(datetime.datetime.now()) #返回 2016-08-19 12:47:03.941925
#print(datetime.date.fromtimestamp(time.time()) )  # 時間戳直接轉成日期格式 2016-08-19
# print(datetime.datetime.now() )
# print(datetime.datetime.now() + datetime.timedelta(3)) #當前時間+3天
# print(datetime.datetime.now() + datetime.timedelta(-3)) #當前時間-3天
# print(datetime.datetime.now() + datetime.timedelta(hours=3)) #當前時間+3小時
# print(datetime.datetime.now() + datetime.timedelta(minutes=30)) #當前時間+30分


#
# c_time  = datetime.datetime.now()
# print(c_time.replace(minute=3,hour=2)) #時間替換
datetime模組

二 random模組

 1 import random
 2  
 3 print(random.random())#(0,1)----float    大於0且小於1之間的小數
 4  
 5 print(random.randint(1,3))  #[1,3]    大於等於1且小於等於3之間的整數
 6  
 7 print(random.randrange(1,3)) #[1,3)    大於等於1且小於3之間的整數
 8  
 9 print(random.choice([1,'23',[4,5]]))#1或者23或者[4,5]
10  
11 print(random.sample([1,'23',[4,5]],2))#列表元素任意2個組合
12  
13 print(random.uniform(1,3))#大於1小於3的小數,如1.927109612082716 
14  
15  
16 item=[1,3,5,7,9]
17 random.shuffle(item) #打亂item的順序,相當於"洗牌"
18 print(item)
import random
def make_code(n):
    res=''
    for i in range(n):
        s1=chr(random.randint(65,90))
        s2=str(random.randint(0,9))
        res+=random.choice([s1,s2])
    return res

print(make_code(9))
生成隨機驗證碼

三 os模組

os模組是與作業系統互動的一個介面

os.getcwd() 獲取當前工作目錄,即當前python指令碼工作的目錄路徑
os.chdir("dirname")  改變當前指令碼工作目錄;相當於shell下cd
os.curdir  返回當前目錄: ('.')
os.pardir  獲取當前目錄的父目錄字串名:('..')
os.makedirs('dirname1/dirname2')    可生成多層遞迴目錄
os.removedirs('dirname1')    若目錄為空,則刪除,並遞迴到上一級目錄,如若也為空,則刪除,依此類推
os.mkdir('dirname')    生成單級目錄;相當於shell中mkdir dirname
os.rmdir('dirname')    刪除單級空目錄,若目錄不為空則無法刪除,報錯;相當於shell中rmdir dirname
os.listdir('dirname')    列出指定目錄下的所有檔案和子目錄,包括隱藏檔案,並以列表方式列印
os.remove()  刪除一個檔案
os.rename("oldname","newname")  重新命名檔案/目錄
os.stat('path/filename')  獲取檔案/目錄資訊
os.sep    輸出作業系統特定的路徑分隔符,win下為"\\",Linux下為"/"
os.linesep    輸出當前平臺使用的行終止符,win下為"\t\n",Linux下為"\n"
os.pathsep    輸出用於分割檔案路徑的字串 win下為;,Linux下為:
os.name    輸出字串指示當前使用平臺。win->'nt'; Linux->'posix'
os.system("bash command")  執行shell命令,直接顯示
os.environ  獲取系統環境變數
os.path.abspath(path)  返回path規範化的絕對路徑
os.path.split(path)  將path分割成目錄和檔名二元組返回
os.path.dirname(path)  返回path的目錄。其實就是os.path.split(path)的第一個元素
os.path.basename(path)  返回path最後的檔名。如何path以/或\結尾,那麼就會返回空值。即os.path.split(path)的第二個元素
os.path.exists(path)  如果path存在,返回True;如果path不存在,返回False
os.path.isabs(path)  如果path是絕對路徑,返回True
os.path.isfile(path)  如果path是一個存在的檔案,返回True。否則返回False
os.path.isdir(path)  如果path是一個存在的目錄,則返回True。否則返回False
os.path.join(path1[, path2[, ...]])  將多個路徑組合後返回,第一個絕對路徑之前的引數將被忽略
os.path.getatime(path)  返回path所指向的檔案或者目錄的最後存取時間
os.path.getmtime(path)  返回path所指向的檔案或者目錄的最後修改時間
os.path.getsize(path) 返回path的大小
View Code
在Linux和Mac平臺上,該函式會原樣返回path,在windows平臺上會將路徑中所有字元轉換為小寫,並將所有斜槓轉換為飯斜槓。
>>> os.path.normcase('c:/windows\\system32\\')   
'c:\\windows\\system32\\'   
   

規範化路徑,如..和/
>>> os.path.normpath('c://windows\\System32\\../Temp/')   
'c:\\windows\\Temp'   

>>> a='/Users/jieli/test1/\\\a1/\\\\aa.py/../..'
>>> print(os.path.normpath(a))
/Users/jieli/test1

os路徑處理
#方式一:推薦使用
import os
#具體應用
import os,sys
possible_topdir = os.path.normpath(os.path.join(
    os.path.abspath(__file__),
    os.pardir, #上一級
    os.pardir,
    os.pardir
))
sys.path.insert(0,possible_topdir)


#方式二:不推薦使用
os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

四 sys模組

1 sys.argv           命令列引數List,第一個元素是程式本身路徑
2 sys.exit(n)        退出程式,正常退出時exit(0)
3 sys.version        獲取Python解釋程式的版本資訊
4 sys.maxint         最大的Int值
5 sys.path           返回模組的搜尋路徑,初始化時使用PYTHONPATH環境變數的值
6 sys.platform       返回作業系統平臺名稱
#=========知識儲備==========
#進度條的效果
[#             ]
[##            ]
[###           ]
[####          ]

#指定寬度
print('[%-15s]' %'#')
print('[%-15s]' %'##')
print('[%-15s]' %'###')
print('[%-15s]' %'####')

#列印%
print('%s%%' %(100)) #第二個%號代表取消第一個%的特殊意義

#可傳參來控制寬度
print('[%%-%ds]' %50) #[%-50s]
print(('[%%-%ds]' %50) %'#')
print(('[%%-%ds]' %50) %'##')
print(('[%%-%ds]' %50) %'###')


#=========實現列印進度條函式==========
import sys
import time

def progress(percent,width=50):
    if percent >= 1:
        percent=1
    show_str=('[%%-%ds]' %width) %(int(width*percent)*'#')
    print('\r%s %d%%' %(show_str,int(100*percent)),file=sys.stdout,flush=True,end='')


#=========應用==========
data_size=1025
recv_size=0
while recv_size < data_size:
    time.sleep(0.1) #模擬資料的傳輸延遲
    recv_size+=1024 #每次收1024

    percent=recv_size/data_size #接收的比例
    progress(percent,width=70) #進度條的寬度70
列印進度條

五 shutil模組

高階的 檔案、資料夾、壓縮包 處理模組

shutil.copyfileobj(fsrc, fdst[, length])
將檔案內容拷貝到另一個檔案中

1 import shutil
2  
3 shutil.copyfileobj(open('old.xml','r'), open('new.xml', 'w'))

shutil.copyfile(src, dst)
拷貝檔案

1 shutil.copyfile('f1.log', 'f2.log') #目標檔案無需存在

shutil.copymode(src, dst)
僅拷貝許可權。內容、組、使用者均不變

1 shutil.copymode('f1.log', 'f2.log') #目標檔案必須存在

shutil.copystat(src, dst)
僅拷貝狀態的資訊,包括:mode bits, atime, mtime, flags

1 shutil.copystat('f1.log', 'f2.log') #目標檔案必須存在

shutil.copy(src, dst)
拷貝檔案和許可權

1 import shutil
2  
3 shutil.copy('f1.log', 'f2.log')

shutil.copy2(src, dst)
拷貝檔案和狀態資訊

1 import shutil
2  
3 shutil.copy2('f1.log', 'f2.log')

shutil.ignore_patterns(*patterns)
shutil.copytree(src, dst, symlinks=False, ignore=None)
遞迴的去拷貝資料夾

1 import shutil
2  
3 shutil.copytree('folder1', 'folder2', ignore=shutil.ignore_patterns('*.pyc', 'tmp*')) #目標目錄不能存在,注意對folder2目錄父級目錄要有可寫許可權,ignore的意思是排除
import shutil

shutil.copytree('f1', 'f2', symlinks=True, ignore=shutil.ignore_patterns('*.pyc', 'tmp*'))

'''
通常的拷貝都把軟連線拷貝成硬連結,即對待軟連線來說,建立新的檔案
'''
拷貝軟連線

shutil.rmtree(path[, ignore_errors[, onerror]])
遞迴的去刪除檔案

1 import shutil
2  
3 shutil.rmtree('folder1')

shutil.move(src, dst)
遞迴的去移動檔案,它類似mv命令,其實就是重新命名。

1 import shutil
2  
3 shutil.move('folder1', 'folder3')

shutil.make_archive(base_name, format,...)

建立壓縮包並返回檔案路徑,例如:zip、tar

建立壓縮包並返回檔案路徑,例如:zip、tar

  • base_name: 壓縮包的檔名,也可以是壓縮包的路徑。只是檔名時,則儲存至當前目錄,否則儲存至指定路徑,
    如 data_bak =>儲存至當前路徑
    如:/tmp/data_bak =>儲存至/tmp/
  • format: 壓縮包種類,“zip”, “tar”, “bztar”,“gztar”
  • root_dir: 要壓縮的資料夾路徑(預設當前目錄)
  • owner: 使用者,預設當前使用者
  • group: 組,預設當前組
  • logger: 用於記錄日誌,通常是logging.Logger物件
1 #將 /data 下的檔案打包放置當前程式目錄
2 import shutil
3 ret = shutil.make_archive("data_bak", 'gztar', root_dir='/data')
4   
5   
6 #將 /data下的檔案打包放置 /tmp/目錄
7 import shutil
8 ret = shutil.make_archive("/tmp/data_bak", 'gztar', root_dir='/data')

shutil 對壓縮包的處理是呼叫 ZipFile 和 TarFile 兩個模組來進行的,詳細:

import zipfile

# 壓縮
z = zipfile.ZipFile('laxi.zip', 'w')
z.write('a.log')
z.write('data.data')
z.close()

# 解壓
z = zipfile.ZipFile('laxi.zip', 'r')
z.extractall(path='.')
z.close()
zipfile壓縮解壓縮
import tarfile

# 壓縮
>>> t=tarfile.open('/tmp/egon.tar','w')
>>> t.add('/test1/a.py',arcname='a.bak')
>>> t.add('/test1/b.py',arcname='b.bak')
>>> t.close()


# 解壓
>>> t=tarfile.open('/tmp/egon.tar','r')
>>> t.extractall('/egon')
>>> t.close()
tarfile壓縮解壓縮

六 json&pickle模組

之前我們學習過用eval內建方法可以將一個字串轉成python物件,不過,eval方法是有侷限性的,對於普通的資料型別,json.loads和eval都能用,但遇到特殊型別的時候,eval就不管用了,所以eval的重點還是通常用來執行一個字串表示式,並返回表示式的值。

1 import json
2 x="[null,true,false,1]"
3 print(eval(x)) #報錯,無法解析null型別,而json就可以
4 print(json.loads(x))

什麼是序列化?

我們把物件(變數)從記憶體中變成可儲存或傳輸的過程稱之為序列化,在Python中叫pickling,在其他語言中也被稱之為serialization,marshalling,flattening等等,都是一個意思。

為什麼要序列化?

1:持久儲存狀態

需知一個軟體/程式的執行就在處理一系列狀態的變化,在程式語言中,'狀態'會以各種各樣有結構的資料型別(也可簡單的理解為變數)的形式被儲存在記憶體中。

記憶體是無法永久儲存資料的,當程式運行了一段時間,我們斷電或者重啟程式,記憶體中關於這個程式的之前一段時間的資料(有結構)都被清空了。

在斷電或重啟程式之前將程式當前記憶體中所有的資料都儲存下來(儲存到檔案中),以便於下次程式執行能夠從檔案中載入之前的資料,然後繼續執行,這就是序列化。

具體的來說,你玩使命召喚闖到了第13關,你儲存遊戲狀態,關機走人,下次再玩,還能從上次的位置開始繼續闖關。或如,虛擬機器狀態的掛起等。

2:跨平臺資料互動

序列化之後,不僅可以把序列化後的內容寫入磁碟,還可以通過網路傳輸到別的機器上,如果收發的雙方約定好實用一種序列化的格式,那麼便打破了平臺/語言差異化帶來的限制,實現了跨平臺資料互動。

反過來,把變數內容從序列化的物件重新讀到記憶體裡稱之為反序列化,即unpickling。

如何序列化之json和pickle:

json

如果我們要在不同的程式語言之間傳遞物件,就必須把物件序列化為標準格式,比如XML,但更好的方法是序列化為JSON,因為JSON表示出來就是一個字串,可以被所有語言讀取,也可以方便地儲存到磁碟或者通過網路傳輸。JSON不僅是標準格式,並且比XML更快,而且可以直接在Web頁面中讀取,非常方便。

JSON表示的物件就是標準的JavaScript語言的物件,JSON和Python內建的資料型別對應如下:

 1 import json
 2  
 3 dic={'name':'alvin','age':23,'sex':'male'}
 4 print(type(dic))#<class 'dict'>
 5  
 6 j=json.dumps(dic)
 7 print(type(j))#<class 'str'>
 8  
 9  
10 f=open('序列化物件','w')
11 f.write(j)  #-------------------等價於json.dump(dic,f)
12 f.close()
13 #-----------------------------反序列化<br>
14 import json
15 f=open('序列化物件')
16 data=json.loads(f.read())#  等價於data=json.load(f)
import json
#dct="{'1':111}"#json 不認單引號
#dct=str({"1":111})#報錯,因為生成的資料還是單引號:{'one': 1}

dct='{"1":"111"}'
print(json.loads(dct))

#conclusion:
#        無論資料是怎樣建立的,只要滿足json格式,就可以json.loads出來,不一定非要dumps的資料才能loads
注意點
# 在python直譯器2.7與3.6之後都可以json.loads(bytes型別),但唯獨3.5不可以
>>> import json
>>> json.loads(b'{"a":111}')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/linhaifeng/anaconda3/lib/python3.5/json/__init__.py", line 312, in loads
    s.__class__.__name__))
TypeError: the JSON object must be str, not 'bytes'
瞭解
# 一.什麼是猴子補丁?
      屬性在執行時的動態替換,叫做猴子補丁(Monkey Patch)。
      猴子補丁的核心就是用自己的程式碼替換所用模組的原始碼,詳細地如下
  1,這個詞原來為Guerrilla Patch,雜牌軍、游擊隊,說明這部分不是原裝的,在英文裡guerilla發音和gorllia(猩猩)相似,再後來就寫了monkey(猴子)。
  2,還有一種解釋是說由於這種方式將原來的程式碼弄亂了(messing with it),在英文裡叫monkeying about(頑皮的),所以叫做Monkey Patch。


# 二. 猴子補丁的功能(一切皆物件)
  1.擁有在模組執行時替換的功能, 例如: 一個函式物件賦值給另外一個函式物件(把函式原本的執行的功能給替換了)
class Monkey:
    def hello(self):
        print('hello')

    def world(self):
        print('world')


def other_func():
    print("from other_func")



monkey = Monkey()
monkey.hello = monkey.world
monkey.hello()
monkey.world = other_func
monkey.world()

# 三.monkey patch的應用場景
如果我們的程式中已經基於json模組編寫了大量程式碼了,發現有一個模組ujson比它效能更高,
但用法一樣,我們肯定不會想所有的程式碼都換成ujson.dumps或者ujson.loads,那我們可能
會想到這麼做
import ujson as json,但是這麼做的需要每個檔案都重新匯入一下,維護成本依然很高
此時我們就可以用到猴子補丁了
只需要在入口處加上
, 只需要在入口加上:

import json
import ujson

def monkey_patch_json():
    json.__name__ = 'ujson'
    json.dumps = ujson.dumps
    json.loads = ujson.loads

monkey_patch_json() # 之所以在入口處加,是因為模組在匯入一次後,後續的匯入便直接引用第一次的成果

#其實這種場景也比較多, 比如我們引用團隊通用庫裡的一個模組, 又想豐富模組的功能, 除了繼承之外也可以考慮用Monkey
Patch.採用猴子補丁之後,如果發現ujson不符合預期,那也可以快速撤掉補丁。個人感覺Monkey
Patch帶了便利的同時也有搞亂原始碼的風險!
    
猴子補丁與ujson

pickle

 1 import pickle
 2  
 3 dic={'name':'alvin','age':23,'sex':'male'}
 4  
 5 print(type(dic))#<class 'dict'>
 6  
 7 j=pickle.dumps(dic)
 8 print(type(j))#<class 'bytes'>
 9  
10  
11 f=open('序列化物件_pickle','wb')#注意是w是寫入str,wb是寫入bytes,j是'bytes'
12 f.write(j)  #-------------------等價於pickle.dump(dic,f)
13  
14 f.close()
15 #-------------------------反序列化
16 import pickle
17 f=open('序列化物件_pickle','rb')
18  
19 data=pickle.loads(f.read())#  等價於data=pickle.load(f)
20  
21  
22 print(data['age'])   
# coding:utf-8
import pickle

with open('a.pkl',mode='wb') as f:
    # 一:在python3中執行的序列化操作如何相容python2
    # python2不支援protocol>2,預設python3中protocol=4
    # 所以在python3中dump操作應該指定protocol=2
    pickle.dump('你好啊',f,protocol=2)

with open('a.pkl', mode='rb') as f:
    # 二:python2中反序列化才能正常使用
    res=pickle.load(f)
    print(res)
python2與python3的pickle相容性問題

Pickle的問題和所有其他程式語言特有的序列化問題一樣,就是它只能用於Python,並且可能不同版本的Python彼此都不相容,因此,只能用Pickle儲存那些不重要的資料,不能成功地反序列化也沒關係。

七 shelve模組

shelve模組比pickle模組簡單,只有一個open函式,返回類似字典的物件,可讀可寫;key必須為字串,而值可以是python所支援的資料型別

import shelve

f=shelve.open(r'sheve.txt')
# f['stu1_info']={'name':'egon','age':18,'hobby':['piao','smoking','drinking']}
# f['stu2_info']={'name':'gangdan','age':53}
# f['school_info']={'website':'http://www.pypy.org','city':'beijing'}

print(f['stu1_info']['hobby'])
f.close()

八 xml模組

xml是實現不同語言或程式之間進行資料交換的協議,跟json差不多,但json使用起來更簡單,不過,古時候,在json還沒誕生的黑暗年代,大家只能選擇用xml呀,至今很多傳統公司如金融行業的很多系統的介面還主要是xml。

xml的格式如下,就是通過<>節點來區別資料結構的:

<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank updated="yes">2</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank updated="yes">5</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank updated="yes">69</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>
xml資料

xml協議在各個語言裡的都 是支援的,在python中可以用以下模組操作xml:

# print(root.iter('year')) #全文搜尋
# print(root.find('country')) #在root的子節點找,只找一個
# print(root.findall('country')) #在root的子節點找,找所有

import xml.etree.ElementTree as ET
 
tree = ET.parse("xmltest.xml")
root = tree.getroot()
print(root.tag)
 
#遍歷xml文件
for child in root:
    print('========>',child.tag,child.attrib,child.attrib['name'])
    for i in child:
        print(i.tag,i.attrib,i.text)
 
#只遍歷year 節點
for node in root.iter('year'):
    print(node.tag,node.text)
#---------------------------------------

import xml.etree.ElementTree as ET
 
tree = ET.parse("xmltest.xml")
root = tree.getroot()
 
#修改
for node in root.iter('year'):
    new_year=int(node.text)+1
    node.text=str(new_year)
    node.set('updated','yes')
    node.set('version','1.0')
tree.write('test.xml')
 
 
#刪除node
for country in root.findall('country'):
   rank = int(country.find('rank').text)
   if rank > 50:
     root.remove(country)
 
tree.write('output.xml')
View Code
#在country內新增(append)節點year2
import xml.etree.ElementTree as ET
tree = ET.parse("a.xml")
root=tree.getroot()
for country in root.findall('country'):
    for year in country.findall('year'):
        if int(year.text) > 2000:
            year2=ET.Element('year2')
            year2.text='新年'
            year2.attrib={'update':'yes'}
            country.append(year2) #往country節點下新增子節點

tree.write('a.xml.swap')

自己建立xml文件:

import xml.etree.ElementTree as ET
 
 
new_xml = ET.Element("namelist")
name = ET.SubElement(new_xml,"name",attrib={"enrolled":"yes"})
age = ET.SubElement(name,"age",attrib={"checked":"no"})
sex = ET.SubElement(name,"sex")
sex.text = '33'
name2 = ET.SubElement(new_xml,"name",attrib={"enrolled":"no"})
age = ET.SubElement(name2,"age")
age.text = '19'
 
et = ET.ElementTree(new_xml) #生成文件物件
et.write("test.xml", encoding="utf-8",xml_declaration=True)
 
ET.dump(new_xml) #列印生成的格式
View Code

九 configparser模組

配置檔案如下:

# 註釋1
; 註釋2

[section1]
k1 = v1
k2:v2
user=egon
age=18
is_admin=true
salary=31

[section2] k1
= v1

讀取

import configparser

config=configparser.ConfigParser()
config.read('a.cfg')

#檢視所有的標題
res=config.sections() #['section1', 'section2']
print(res)

#檢視標題section1下所有key=value的key
options=config.options('section1')
print(options) #['k1', 'k2', 'user', 'age', 'is_admin', 'salary']

#檢視標題section1下所有key=value的(key,value)格式
item_list=config.items('section1')
print(item_list) #[('k1', 'v1'), ('k2', 'v2'), ('user', 'egon'), ('age', '18'), ('is_admin', 'true'), ('salary', '31')]

#檢視標題section1下user的值=>字串格式
val=config.get('section1','user')
print(val) #egon

#檢視標題section1下age的值=>整數格式
val1=config.getint('section1','age')
print(val1) #18

#檢視標題section1下is_admin的值=>布林值格式
val2=config.getboolean('section1','is_admin')
print(val2) #True

#檢視標題section1下salary的值=>浮點型格式
val3=config.getfloat('section1','salary')
print(val3) #31.0

改寫

import configparser

config=configparser.ConfigParser()
config.read('a.cfg',encoding='utf-8')


#刪除整個標題section2
config.remove_section('section2')

#刪除標題section1下的某個k1和k2
config.remove_option('section1','k1')
config.remove_option('section1','k2')

#判斷是否存在某個標題
print(config.has_section('section1'))

#判斷標題section1下是否有user
print(config.has_option('section1',''))


#新增一個標題
config.add_section('egon')

#在標題egon下新增name=egon,age=18的配置
config.set('egon','name','egon')
config.set('egon','age',18) #報錯,必須是字串


#最後將修改的內容寫入檔案,完成最終的修改
config.write(open('a.cfg','w'))
import configparser
  
config = configparser.ConfigParser()
config["DEFAULT"] = {'ServerAliveInterval': '45',
                      'Compression': 'yes',
                     'CompressionLevel': '9'}
  
config['bitbucket.org'] = {}
config['bitbucket.org']['User'] = 'hg'
config['topsecret.server.com'] = {}
topsecret = config['topsecret.server.com']
topsecret['Host Port'] = '50022'     # mutates the parser
topsecret['ForwardX11'] = 'no'  # same here
config['DEFAULT']['ForwardX11'] = 'yes'
with open('example.ini', 'w') as configfile:
   config.write(configfile)
基於上述方法新增一個ini文件

十 hashlib模組

# 1、什麼叫hash:hash是一種演算法(3.x裡代替了md5模組和sha模組,主要提供 SHA1, SHA224, SHA256, SHA384, SHA512 ,MD5 演算法),該演算法接受傳入的內容,經過運算得到一串hash值
# 2、hash值的特點是:
#2.1 只要傳入的內容一樣,得到的hash值必然一樣=====>要用明文傳輸密碼檔案完整性校驗
#2.2 不能由hash值返解成內容=======》把密碼做成hash值,不應該在網路傳輸明文密碼
#2.3 只要使用的hash演算法不變,無論校驗的內容有多大,得到的hash值長度是固定的

hash演算法就像一座工廠,工廠接收你送來的原材料(可以用m.update()為工廠運送原材料),經過加工返回的產品就是hash值

 1 import hashlib
 2  
 3 m=hashlib.md5()# m=hashlib.sha256()
 4  
 5 m.update('hello'.encode('utf8'))
 6 print(m.hexdigest())  #5d41402abc4b2a76b9719d911017c592
 7  
 8 m.update('alvin'.encode('utf8'))
 9  
10 print(m.hexdigest())  #92a7e713c30abbb0319fa07da2a5c4af
11  
12 m2=hashlib.md5()
13 m2.update('helloalvin'.encode('utf8'))
14 print(m2.hexdigest()) #92a7e713c30abbb0319fa07da2a5c4af
15 
16 '''
17 注意:把一段很長的資料update多次,與一次update這段長資料,得到的結果一樣
18 但是update多次為校驗大檔案提供了可能。
19 '''

以上加密演算法雖然依然非常厲害,但時候存在缺陷,即:通過撞庫可以反解。所以,有必要對加密演算法中新增自定義key再來做加密。

1 import hashlib
2  
3 # ######## 256 ########
4  
5 hash = hashlib.sha256('898oaFs09f'.encode('utf8'))
6 hash.update('alvin'.encode('utf8'))
7 print (hash.hexdigest())#e79e68f070cdedcfe63eaf1a2e92c83b4cfb1b5c6bc452d214c1b7e77cdfd1c7
import hashlib
passwds=[
    'alex3714',
    'alex1313',
    'alex94139413',
    'alex123456',
    '123456alex',
    'a123lex',
    ]
def make_passwd_dic(passwds):
    dic={}
    for passwd in passwds:
        m=hashlib.md5()
        m.update(passwd.encode('utf-8'))
        dic[passwd]=m.hexdigest()
    return dic

def break_code(cryptograph,passwd_dic):
    for k,v in passwd_dic.items():
        if v == cryptograph:
            print('密碼是===>\033[46m%s\033[0m' %k)

cryptograph='aee949757a2e698417463d47acac93df'
break_code(cryptograph,make_passwd_dic(passwds))
模擬撞庫破解密碼

python 還有一個 hmac 模組,它內部對我們建立 key 和 內容 進行進一步的處理然後再加密:

import hmac
h1=hmac.new('hello'.encode('utf-8'),digestmod='md5')
h1.update('world'.encode('utf-8'))

print(h1.hexdigest())
#要想保證hmac最終結果一致,必須保證:
#1:hmac.new括號內指定的初始key一樣
#2:無論update多少次,校驗的內容累加到一起是一樣的內容

# 操作一
import hmac
h1=hmac.new('hello'.encode('utf-8'),digestmod='md5')
h1.update('world'.encode('utf-8'))

print(h1.hexdigest()) # 0e2564b7e100f034341ea477c23f283b

# 操作二
import hmac
h2=hmac.new('hello'.encode('utf-8'),digestmod='md5')
h2.update('w'.encode('utf-8'))
h2.update('orld'.encode('utf-8'))

print(h1.hexdigest()) # 0e2564b7e100f034341ea477c23f283b
注意!注意!注意

十一 suprocess模組

 1 import  subprocess
 2 
 3 '''
 4 sh-3.2# ls /Users/egon/Desktop |grep txt$
 5 mysql.txt
 6 tt.txt
 7 事物.txt
 8 '''
 9 
10 res1=subprocess.Popen('ls /Users/jieli/Desktop',shell=True,stdout=subprocess.PIPE)
11 res=subprocess.Popen('grep txt$',shell=True,stdin=res1.stdout,
12                  stdout=subprocess.PIPE)
13 
14 print(res.stdout.read().decode('utf-8'))
15 
16 
17 #等同於上面,但是上面的優勢在於,一個數據流可以和另外一個數據流互動,可以通過爬蟲得到結果然後交給grep
18 res1=subprocess.Popen('ls /Users/jieli/Desktop |grep txt$',shell=True,stdout=subprocess.PIPE)
19 print(res1.stdout.read().decode('utf-8'))
20 
21 
22 #windows下:
23 # dir | findstr 'test*'
24 # dir | findstr 'txt$'
25 import subprocess
26 res1=subprocess.Popen(r'dir C:\Users\Administrator\PycharmProjects\test\函式備課',shell=True,stdout=subprocess.PIPE)
27 res=subprocess.Popen('findstr test*',shell=True,stdin=res1.stdout,
28                  stdout=subprocess.PIPE)
29 
30 print(res.stdout.read().decode('gbk')) #subprocess使用當前系統預設編碼,得到結果為bytes型別,在windows下需要用gbk解碼

詳細參考官網

十二 logging模組

一 日誌級別

CRITICAL = 50 #FATAL = CRITICAL
ERROR = 40
WARNING = 30 #WARN = WARNING
INFO = 20
DEBUG = 10
NOTSET = 0 #不設定

二 預設級別為warning,預設列印到終端

import logging

logging.debug('除錯debug')
logging.info('訊息info')
logging.warning('警告warn')
logging.error('錯誤error')
logging.critical('嚴重critical')

'''
WARNING:root:警告warn
ERROR:root:錯誤error
CRITICAL:root:嚴重critical
'''

三 為logging模組指定全域性配置,針對所有logger有效,控制列印到檔案中

可在logging.basicConfig()函式中通過具體引數來更改logging模組預設行為,可用引數有
filename:用指定的檔名建立FiledHandler(後邊會具體講解handler的概念),這樣日誌會被儲存在指定的檔案中。
filemode:檔案開啟方式,在指定了filename時使用這個引數,預設值為“a”還可指定為“w”。
format:指定handler使用的日誌顯示格式。 
datefmt:指定日期時間格式。 
level:設定rootlogger(後邊會講解具體概念)的日誌級別 
stream:用指定的stream建立StreamHandler。可以指定輸出到sys.stderr,sys.stdout或者檔案,預設為sys.stderr。若同時列出了filename和stream兩個引數,則stream引數會被忽略。



#格式
%(name)s:Logger的名字,並非使用者名稱,詳細檢視

%(levelno)s:數字形式的日誌級別

%(levelname)s:文字形式的日誌級別

%(pathname)s:呼叫日誌輸出函式的模組的完整路徑名,可能沒有

%(filename)s:呼叫日誌輸出函式的模組的檔名

%(module)s:呼叫日誌輸出函式的模組名

%(funcName)s:呼叫日誌輸出函式的函式名

%(lineno)d:呼叫日誌輸出函式的語句所在的程式碼行

%(created)f:當前時間,用UNIX標準的表示時間的浮 點數表示

%(relativeCreated)d:輸出日誌資訊時的,自Logger建立以 來的毫秒數

%(asctime)s:字串形式的當前時間。預設格式是 “2003-07-08 16:49:45,896”。逗號後面的是毫秒

%(thread)d:執行緒ID。可能沒有

%(threadName)s:執行緒名。可能沒有

%(process)d:程序ID。可能沒有

%(message)s:使用者輸出的訊息

 
logging.basicConfig()
#======介紹
可在logging.basicConfig()函式中可通過具體引數來更改logging模組預設行為,可用引數有
filename:用指定的檔名建立FiledHandler(後邊會具體講解handler的概念),這樣日誌會被儲存在指定的檔案中。
filemode:檔案開啟方式,在指定了filename時使用這個引數,預設值為“a”還可指定為“w”。
format:指定handler使用的日誌顯示格式。
datefmt:指定日期時間格式。
level:設定rootlogger(後邊會講解具體概念)的日誌級別
stream:用指定的stream建立StreamHandler。可以指定輸出到sys.stderr,sys.stdout或者檔案,預設為sys.stderr。若同時列出了filename和stream兩個引數,則stream引數會被忽略。


format引數中可能用到的格式化串:
%(name)s Logger的名字
%(levelno)s 數字形式的日誌級別
%(levelname)s 文字形式的日誌級別
%(pathname)s 呼叫日誌輸出函式的模組的完整路徑名,可能沒有
%(filename)s 呼叫日誌輸出函式的模組的檔名
%(module)s 呼叫日誌輸出函式的模組名
%(funcName)s 呼叫日誌輸出函式的函式名
%(lineno)d 呼叫日誌輸出函式的語句所在的程式碼行
%(created)f 當前時間,用UNIX標準的表示時間的浮 點數表示
%(relativeCreated)d 輸出日誌資訊時的,自Logger建立以 來的毫秒數
%(asctime)s 字串形式的當前時間。預設格式是 “2003-07-08 16:49:45,896”。逗號後面的是毫秒
%(thread)d 執行緒ID。可能沒有
%(threadName)s 執行緒名。可能沒有
%(process)d 程序ID。可能沒有
%(message)s使用者輸出的訊息




#========使用
import logging
logging.basicConfig(filename='access.log',
                    format='%(asctime)s - %(name)s - %(levelname)s -%(module)s:  %(message)s',
                    datefmt='%Y-%m-%d %H:%M:%S %p',
                    level=10)

logging.debug('除錯debug')
logging.info('訊息info')
logging.warning('警告warn')
logging.error('錯誤error')
logging.critical('嚴重critical')





#========結果
access.log內容:
2017-07-28 20:32:17 PM - root - DEBUG -test:  除錯debug
2017-07-28 20:32:17 PM - root - INFO -test:  訊息info
2017-07-28 20:32:17 PM - root - WARNING -test:  警告warn
2017-07-28 20:32:17 PM - root - ERROR -test:  錯誤error
2017-07-28 20:32:17 PM - root - CRITICAL -test:  嚴重critical

part2: 可以為logging模組指定模組級的配置,即所有logger的配置
View Code

四 logging模組的Formatter,Handler,Logger,Filter物件

原理圖:https://pan.baidu.com/s/1skWyTT7

#logger:產生日誌的物件

#Filter:過濾日誌的物件

#Handler:接收日誌然後控制列印到不同的地方,FileHandler用來列印到檔案中,StreamHandler用來列印到終端

#Formatter物件:可以定製不同的日誌格式物件,然後繫結給不同的Handler物件使用,以此來控制不同的Handler的日誌格式
'''
critical=50
error =40
warning =30
info = 20
debug =10
'''


import logging

#1、logger物件:負責產生日誌,然後交給Filter過濾,然後交給不同的Handler輸出
logger=logging.getLogger(__file__)

#2、Filter物件:不常用,略

#3、Handler物件:接收logger傳來的日誌,然後控制輸出
h1=logging.FileHandler('t1.log') #列印到檔案
h2=logging.FileHandler('t2.log') #列印到檔案
h3=logging.StreamHandler() #列印到終端

#4、Formatter物件:日誌格式
formmater1=logging.Formatter('%(asctime)s - %(name)s - %(levelname)s -%(module)s:  %(message)s',
                    datefmt='%Y-%m-%d %H:%M:%S %p',)

formmater2=logging.Formatter('%(asctime)s :  %(message)s',
                    datefmt='%Y-%m-%d %H:%M:%S %p',)

formmater3=logging.Formatter('%(name)s %(message)s',)


#5、為Handler物件繫結格式
h1.setFormatter(formmater1)
h2.setFormatter(formmater2)
h3.setFormatter(formmater3)

#6、將Handler新增給logger並設定日誌級別
logger.addHandler(h1)
logger.addHandler(h2)
logger.addHandler(h3)
logger.setLevel(10)

#7、測試
logger.debug('debug')
logger.info('info')
logger.warning('warning')
logger.error('error')
logger.critical('critical')
View Code

五 Logger與Handler的級別

logger是第一級過濾,然後才能到handler,我們可以給logger和handler同時設定level,但是需要注意的是

Logger is also the first to filter the message based on a level — if you set the logger to INFO, and all handlers to DEBUG, you still won't receive DEBUG messages on handlers — they'll be rejected by the logger itself. If you set logger to DEBUG, but all handlers to INFO, you won't receive any DEBUG messages either — because while the logger says "ok, process this", the handlers reject it (DEBUG < INFO).



#驗證
import logging


form=logging.Formatter('%(asctime)s - %(name)s - %(levelname)s -%(module)s:  %(message)s',
                    datefmt='%Y-%m-%d %H:%M:%S %p',)

ch=logging.StreamHandler()

ch.setFormatter(form)
# ch.setLevel(10)
ch.setLevel(20)

l1=logging.getLogger('root')
# l1.setLevel(20)
l1.setLevel(10)
l1.addHandler(ch)

l1.debug('l1 debug')
重要,重要,重要!!!

六 Logger的繼承(瞭解)

import logging

formatter=logging.Formatter('%(asctime)s - %(name)s - %(levelname)s -%(module)s:  %(message)s',
                    datefmt='%Y-%m-%d %H:%M:%S %p',)

ch=logging.StreamHandler()
ch.setFormatter(formatter)


logger1=logging.getLogger('root')
logger2=logging.getLogger('root.child1')
logger3=logging.getLogger('root.child1.child2')


logger1.addHandler(ch)
logger2.addHandler(ch)
logger3.addHandler(ch)
logger1.setLevel(10)
logger2.setLevel(10)
logger3.setLevel(10)

logger1.debug('log1 debug')
logger2.debug('log2 debug')
logger3.debug('log3 debug')
'''
2017-07-28 22:22:05 PM - root - DEBUG -test:  log1 debug
2017-07-28 22:22:05 PM - root.child1 - DEBUG -test:  log2 debug
2017-07-28 22:22:05 PM - root.child1 - DEBUG -test:  log2 debug
2017-07-28 22:22:05 PM - root.child1.child2 - DEBUG -test:  log3 debug
2017-07-28 22:22:05 PM - root.child1.child2 - DEBUG -test:  log3 debug
2017-07-28 22:22:05 PM - root.child1.child2 - DEBUG -test:  log3 debug
'''
View Code

七 應用

"""
logging配置
"""

import os
import logging.config

# 定義三種日誌輸出格式 開始

standard_format = '[%(asctime)s][%(threadName)s:%(thread)d][task_id:%(name)s][%(filename)s:%(lineno)d]' \
                  '[%(levelname)s][%(message)s]' #其中name為getlogger指定的名字

simple_format = '[%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d]%(message)s'

id_simple_format = '[%(levelname)s][%(asctime)s] %(message)s'

# 定義日誌輸出格式 結束

logfile_dir = os.path.dirname(os.path.abspath(__file__))  # log檔案的目錄

logfile_name = 'all2.log'  # log檔名

# 如果不存在定義的日誌目錄就建立一個
if not os.path.isdir(logfile_dir):
    os.mkdir(logfile_dir)

# log檔案的全路徑
logfile_path = os.path.join(logfile_dir, logfile_name)

# log配置字典
LOGGING_DIC = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'standard': {
            'format': standard_format
        },
        'simple': {
            'format': simple_format
        },
    },
    'filters': {},
    'handlers': {
        #列印到終端的日誌
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',  # 列印到螢幕
            'formatter': 'simple'
        },
        #列印到檔案的日誌,收集info及以上的日誌
        'default': {
            'level': 'DEBUG',
            'class': 'logging.handlers.RotatingFileHandler',  # 儲存到檔案
            'formatter': 'standard',
            'filename': logfile_path,  # 日誌檔案
            'maxBytes': 1024*1024*5,  # 日誌大小 5M
            'backupCount': 5,
            'encoding': 'utf-8',  # 日誌檔案的編碼,再也不用擔心中文log亂碼了
        },
    },
    'loggers': {
        #logging.getLogger(__name__)拿到的logger配置
        '': {
            'handlers': ['default', 'console'],  # 這裡把上面定義的兩個handler都加上,即log資料既寫入檔案又列印到螢幕
            'level': 'DEBUG',
            'propagate': True,  # 向上(更高level的logger)傳遞
        },
    },
}


def load_my_logging_cfg():
    logging.config.dictConfig(LOGGING_DIC)  # 匯入上面定義的logging配置
    logger = logging.getLogger(__name__)  # 生成一個log例項
    logger.info('It works!')  # 記錄該檔案的執行狀態

if __name__ == '__main__':
    load_my_logging_cfg()
logging配置檔案
"""
MyLogging Test
"""

import time
import logging
import my_logging  # 匯入自定義的logging配置

logger = logging.getLogger(__name__)  # 生成logger例項


def demo():
    logger.debug("start range... time:{}".format(time.time()))
    logger.info("中文測試開始。。。")
    for i in range(10):
        logger.debug("i:{}".format(i))
        time.sleep(0.2)
    else:
        logger.debug("over range... time:{}".format(time.time()))
    logger.info("中文測試結束。。。")

if __name__ == "__main__":
    my_logging.load_my_logging_cfg()  # 在你程式檔案的入口載入自定義logging配置
    demo()
使用
注意注意注意:


#1、有了上述方式我們的好處是:所有與logging模組有關的配置都寫到字典中就可以了,更加清晰,方便管理


#2、我們需要解決的問題是:
    1、從字典載入配置:logging.config.dictConfig(settings.LOGGING_DIC)

    2、拿到logger物件來產生日誌
    logger物件都是配置到字典的loggers 鍵對應的子字典中的
    按照我們對logging模組的理解,要想獲取某個東西都是通過名字,也就是key來獲取的
    於是我們要獲取不同的logger物件就是
    logger=logging.getLogger('loggers子字典的key名')

    
    但問題是:如果我們想要不同logger名的logger物件都共用一段配置,那麼肯定不能在loggers子字典中定義n個key   
 'loggers': {    
        'l1': {
            'handlers': ['default', 'console'],  #
            'level': 'DEBUG',
            'propagate': True,  # 向上(更高level的logger)傳遞
        },
        'l2: {
            'handlers': ['default', 'console' ], 
            'level': 'DEBUG',
            'propagate': False,  # 向上(更高level的logger)傳遞
        },
        'l3': {
            'handlers': ['default', 'console'],  #
            'level': 'DEBUG',
            'propagate': True,  # 向上(更高level的logger)傳遞
        },

}

    
#我們的解決方式是,定義一個空的key
    'loggers': {
        '': {
            'handlers': ['default', 'console'], 
            'level': 'DEBUG',
            'propagate': True, 
        },

}

這樣我們再取logger物件時
logging.getLogger(__name__),不同的檔案__name__不同,這保證了列印日誌時標識資訊不同,但是拿著該名字去loggers裡找key名時卻發現找不到,於是預設使用key=''的配置
!!!關於如何拿到logger物件的詳細解釋!!!
另外一個django的配置,瞄一眼就可以,跟上面的一樣
#logging_config.py
LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'standard': {
            'format': '[%(asctime)s][%(threadName)s:%(thread)d][task_id:%(name)s][%(filename)s:%(lineno)d]'
                      '[%(levelname)s][%(message)s]'
        },
        'simple': {
            'format': '[%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d]%(message)s'
        },
        'collect': {
            'format': '%(message)s'
        }
    },
    'filters': {
        'require_debug_true': {
            '()': 'django.utils.log.RequireDebugTrue',
        },
    },
    'handlers': {
        #列印到終端的日誌
        'console': {
            'level': 'DEBUG',
            'filters': ['require_debug_true'],
            'class': 'logging.StreamHandler',
            'formatter': 'simple'
        },
        #列印到檔案的日誌,收集info及以上的日誌
        'default': {
            'level': 'INFO',
            'class': 'logging.handlers.RotatingFileHandler',  # 儲存到檔案,自動切
            'filename': os.path.join(BASE_LOG_DIR, "xxx_info.log"),  # 日誌檔案
            'maxBytes': 1024 * 1024 * 5,  # 日誌大小 5M
            'backupCount': 3,
            'formatter': 'standard',
            'encoding': 'utf-8',
        },
        #列印到檔案的日誌:收集錯誤及以上的日誌
        'error': {
            'level': 'ERROR',
            'class': 'logging.handlers.RotatingFileHandler',  # 儲存到檔案,自動切
            'filename': os.path.join(BASE_LOG_DIR, "xxx_err.log"),  # 日誌檔案
            'maxBytes': 1024 * 1024 * 5,  # 日誌大小 5M
            'backupCount': 5,
            'formatter': 'standard',
            'encoding': 'utf-8',
        },
        #列印到檔案的日誌
        'collect': {
            'level': 'INFO',
            'class': 'logging.handlers.RotatingFileHandler',  # 儲存到檔案,自動切
            'filename': os.path.join(BASE_LOG_DIR, "xxx_collect.log"),
            'maxBytes': 1024 * 1024 * 5,  # 日誌大小 5M
            'backupCount': 5,
            'formatter': 'collect',
            'encoding': "utf-8"
        }
    },
    'loggers': {
        #logging.getLogger(__name__)拿到的logger配置
        '': {
            'handlers': ['default', 'console', 'error'],
            'level': 'DEBUG',
            'propagate': True,
        },
        #logging.getLogger('collect')拿到的logger配置
        'collect': {
            'handlers': ['console', 'collect'],
            'level': 'INFO',
        }
    },
}


# -----------
# 用法:拿到倆個logger

logger = logging.getLogger(__name__) #線上正常的日誌
collect_logger = logging.getLogger("collect") #領導說,需要為領導們單獨定製領導們看的日誌
View Code

==========================直奔主題版============================

1、日誌級別與配置

import logging

# 一:日誌配置
logging.basicConfig(
    # 1、日誌輸出位置:1、終端 2、檔案
    # filename='access.log', # 不指定,預設列印到終端

    # 2、日誌格式
    format='%(asctime)s - %(name)s - %(levelname)s -%(module)s:  %(message)s',

    # 3、時間格式
    datefmt='%Y-%m-%d %H:%M:%S %p',

    # 4、日誌級別
    # critical => 50
    # error => 40
    # warning => 30
    # info => 20
    # debug => 10
    level=30,
)

# 二:輸出日誌
logging.debug('除錯debug')
logging.info('訊息info')
logging.warning('警告warn')
logging.error('錯誤error')
logging.critical('嚴重critical')

'''
# 注意下面的root是預設的日誌名字
WARNING:root:警告warn
ERROR:root:錯誤error
CRITICAL:root:嚴重critical
'''

2、日誌配置字典

"""
logging配置
"""

import os

# 1、定義三種日誌輸出格式,日誌中可能用到的格式化串如下
# %(name)s Logger的名字
# %(levelno)s 數字形式的日誌級別
# %(levelname)s 文字形式的日誌級別
# %(pathname)s 呼叫日誌輸出函式的模組的完整路徑名,可能沒有
# %(filename)s 呼叫日誌輸出函式的模組的檔名
# %(module)s 呼叫日誌輸出函式的模組名
# %(funcName)s 呼叫日誌輸出函式的函式名
# %(lineno)d 呼叫日誌輸出函式的語句所在的程式碼行
# %(created)f 當前時間,用UNIX標準的表示時間的浮 點數表示
# %(relativeCreated)d 輸出日誌資訊時的,自Logger建立以 來的毫秒數
# %(asctime)s 字串形式的當前時間。預設格式是 “2003-07-08 16:49:45,896”。逗號後面的是毫秒
# %(thread)d 執行緒ID。可能沒有
# %(threadName)s 執行緒名。可能沒有
# %(process)d 程序ID。可能沒有
# %(message)s使用者輸出的訊息

# 2、強調:其中的%(name)s為getlogger時指定的名字
standard_format = '[%(asctime)s][%(threadName)s:%(thread)d][task_id:%(name)s][%(filename)s:%(lineno)d]' \
                  '[%(levelname)s][%(message)s]'

simple_format = '[%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d]%(message)s'

test_format = '%(asctime)s] %(message)s'

# 3、日誌配置字典
LOGGING_DIC = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'standard': {
            'format': standard_format
        },
        'simple': {
            'format': simple_format
        },
        'test': {
            'format': test_format
        },
    },
    'filters': {},
    'handlers': {
        #列印到終端的日誌
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',  # 列印到螢幕
            'formatter': 'simple'
        },
        #列印到檔案的日誌,收集info及以上的日誌
        'default': {
            'level': 'DEBUG',
            'class': 'logging.handlers.RotatingFileHandler',  # 儲存到檔案,日誌輪轉
            'formatter': 'standard',
            # 可以定製日誌檔案路徑
            # BASE_DIR = os.path.dirname(os.path.abspath(__file__))  # log檔案的目錄
            # LOG_PATH = os.path.join(BASE_DIR,'a1.log')
            'filename': 'a1.log',  # 日誌檔案
            'maxBytes': 1024*1024*5,  # 日誌大小 5M
            'backupCount': 5,
            'encoding': 'utf-8',  # 日誌檔案的編碼,再也不用擔心中文log亂碼了
        },
        'other': {
            'level': 'DEBUG',
            'class': 'logging.FileHandler',  # 儲存到檔案
            'formatter': 'test',
            'filename': 'a2.log',
            'encoding': 'utf-8',
        },
    },
    'loggers': {
        #logging.getLogger(__name__)拿到的logger配置
        '': {
            'handlers': ['default', 'console'],  # 這裡把上面定義的兩個handler都加上,即log資料既寫入檔案又列印到螢幕
            'level': 'DEBUG', # loggers(第一層日誌級別關限制)--->handlers(第二層日誌級別關卡限制)
            'propagate': False,  # 預設為True,向上(更高level的logger)傳遞,通常設定為False即可,否則會一份日誌向上層層傳遞
        },
        '專門的採集': {
            'handlers': ['other',],
            'level': 'DEBUG',
            'propagate': False,
        },
    },
}
日誌配置字典LOGGING_DIC

3、使用

import settings

# !!!強調!!!
# 1、logging是一個包,需要使用其下的config、getLogger,可以如下匯入
# from logging import config
# from logging import getLogger

# 2、也可以使用如下匯入
import logging.config # 這樣連同logging.getLogger都一起匯入了,然後使用字首logging.config.

# 3、載入配置
logging.config.dictConfig(settings.LOGGING_DIC)

# 4、輸出日誌
logger1=logging.getLogger('使用者交易')
logger1.info('egon兒子alex轉賬3億冥幣')

# logger2=logging.getLogger('專門的採集') # 名字傳入的必須是'專門的採集',與LOGGING_DIC中的配置唯一對應
# logger2.debug('專門採集的日誌')
common.py

十三 re模組

一:什麼是正則?

 正則就是用一些具有特殊含義的符號組合到一起(稱為正則表示式)來描述字元或者字串的方法。或者說:正則就是用來描述一類事物的規則。(在Python中)它內嵌在Python中,並通過 re 模組實現。正則表示式模式被編譯成一系列的位元組碼,然後由用 C 編寫的匹配引擎執行。

生活中處處都是正則:

比如我們描述:4條腿

  你可能會想到的是四條腿的動物或者桌子,椅子等

繼續描述:4條腿,活的

就只剩下四條腿的動物這一類了

二:常用匹配模式(元字元)

http://blog.csdn.net/yufenghyc/article/details/51078107

# =================================匹配模式=================================
#一對一的匹配
# 'hello'.replace(old,new)
# 'hello'.find('pattern')

#正則匹配
import re
#\w與\W
print(re.findall('\w','hello egon 123')) #['h', 'e', 'l', 'l', 'o', 'e', 'g', 'o', 'n', '1', '2', '3']
print(re.findall('\W','hello egon 123')) #[' ', ' ']

#\s與\S
print(re.findall('\s','hello  egon  123')) #[' ', ' ', ' ', ' ']
print(re.findall('\S','hello  egon  123')) #['h', 'e', 'l', 'l', 'o', 'e', 'g', 'o', 'n', '1', '2', '3']

#\n \t都是空,都可以被\s匹配
print(re.findall('\s','hello \n egon \t 123')) #[' ', '\n', ' ', ' ', '\t', ' ']

#\n與\t
print(re.findall(r'\n','hello egon \n123')) #['\n']
print(re.findall(r'\t','hello egon\t123')) #['\n']

#\d與\D
print(re.findall('\d','hello egon 123')) #['1', '2', '3']
print(re.findall('\D','hello egon 123')) #['h', 'e', 'l', 'l', 'o', ' ', 'e', 'g', 'o', 'n', ' ']

#\A與\Z
print(re.findall('\Ahe','hello egon 123')) #['he'],\A==>^
print(re.findall('123\Z','hello egon 123')) #['he'],\Z==>$

#^與$
print(re.findall('^h','hello egon 123')) #['h']
print(re.findall('3$','hello egon 123')) #['3']

# 重複匹配:| . | * | ? | .* | .*? | + | {n,m} |
#.
print(re.findall('a.b','a1b')) #['a1b']
print(re.findall('a.b','a1b a*b a b aaab')) #['a1b', 'a*b', 'a b', 'aab']
print(re.findall('a.b','a\nb')) #[]
print(re.findall('a.b','a\nb',re.S)) #['a\nb']
print(re.findall('a.b','a\nb',re.DOTALL)) #['a\nb']同上一條意思一樣

#*
print(re.findall('ab*','bbbbbbb')) #[]
print(re.findall('ab*','a')) #['a']
print(re.findall('ab*','abbbb')) #['abbbb']

#?
print(re.findall('ab?','a')) #['a']
print(re.findall('ab?','abbb')) #['ab']
#匹配所有包含小數在內的數字
print(re.findall('\d+\.?\d*',"asdfasdf123as1.13dfa12adsf1asdf3")) #['123', '1.13', '12', '1', '3']

#.*預設為貪婪匹配
print(re.findall('a.*b','a1b22222222b')) #['a1b22222222b']

#.*?為非貪婪匹配:推薦使用
print(re.findall('a.*?b','a1b22222222b')) #['a1b']

#+
print(re.findall('ab+','a')) #[]
print(re.findall('ab+','abbb')) #['abbb']

#{n,m}
print(re.findall('ab{2}','abbb')) #['abb']
print(re.findall('ab{2,4}','abbb')) #['abb']
print(re.findall('ab{1,}','abbb')) #'ab{1,}' ===> 'ab+'
print(re.findall('ab{0,}','abbb')) #'ab{0,}' ===> 'ab*'

#[]
print(re.findall('a[1*-]b','a1b a*b a-b')) #[]內的都為普通字元了,且如果-沒有被轉意的話,應該放到[]的開頭或結尾
print(re.findall('a[^1*-]b','a1b a*b a-b a=b')) #[]內的^代表的意思是取反,所以結果為['a=b']
print(re.findall('a[0-9]b','a1b a*b a-b a=b')) #[]內的^代表的意思是取反,所以結果為['a=b']
print(re.findall('a[a-z]b','a1b a*b a-b a=b aeb')) #[]內的^代表的意思是取反,所以結果為['a=b']
print(re.findall('a[a-zA-Z]b','a1b a*b a-b a=b aeb aEb')) #[]內的^代表的意思是取反,所以結果為['a=b']

#\# print(re.findall('a\\c','a\c')) #對於正則來說a\\c確實可以匹配到a\c,但是在python直譯器讀取a\\c時,會發生轉義,然後交給re去執行,所以丟擲異常
print(re.findall(r'a\\c','a\c')) #r代表告訴直譯器使用rawstring,即原生字串,把我們正則內的所有符號都當普通字元處理,不要轉義
print(re.findall('a\\\\c','a\c')) #同上面的意思一樣,和上面的結果一樣都是['a\\c']

#():分組
print(re.findall('ab+','ababab123')) #['ab', 'ab', 'ab']
print(re.findall('(ab)+123','ababab123')) #['ab'],匹配到末尾的ab123中的ab
print(re.findall('(?:ab)+123','ababab123')) #findall的結果不是匹配的全部內容,而是組內的內容,?:可以讓結果為匹配的全部內容
print(re.findall('href="(.*?)"','<a href="http://www.baidu.com">點選</a>'))#['http://www.baidu.com']
print(re.findall('href="(?:.*?)"','<a href="http://www.baidu.com">點選</a>'))#['href="http://www.baidu.com"']

#|
print(re.findall('compan(?:y|ies)','Too many companies have gone bankrupt, and the next one is my company'))

# ===========================re模組提供的方法介紹===========================
import re
#1
print(re.findall('e','alex make love') )   #['e', 'e', 'e'],返回所有滿足匹配條件的結果,放在列表裡
#2
print(re.search('e','alex make love').group()) #e,只到找到第一個匹配然後返回一個包含匹配資訊的物件,該物件可以通過呼叫group()方法得到匹配的字串,如果字串沒有匹配,則返回None。

#3
print(re.match('e','alex make love'))    #None,同search,不過在字串開始處進行匹配,完全可以用search+^代替match

#4
print(re.split('[ab]','abcd'))     #['', '', 'cd'],先按'a'分割得到''和'bcd',再對''和'bcd'分別按'b'分割

#5
print('===>',re.sub('a','A','alex make love')) #===> Alex mAke love,不指定n,預設替換所有
print('===>',re.sub('a','A','alex make love',1)) #===> Alex make love
print('===>',re.sub('a','A','alex make love',2)) #===> Alex mAke love
print('===>',re.sub('^(\w+)(.*?\s)(\w+)(.*?\s)(\w+)(.*?)$',r'\5\2\3\4\1','alex make love')) #===> love make alex

print('===>',re.subn('a','A','alex make love')) #===> ('Alex mAke love', 2),結果帶有總共替換的個數


#6
obj=re.compile('\d{2}')

print(obj.search('abc123eeee').group()) #12
print(obj.findall('abc123eeee')) #['12'],重用了obj
import re
print(re.findall("<(?P<tag_name>\w+)>\w+</(?P=tag_name)>","<h1>hello</h1>")) #['h1']
print(re.search("<(?P<tag_name>\w+)>\w+</(?P=tag_name)>","<h1>hello</h1>").group()) #<h1>hello</h1>
print(re.search("<(?P<tag_name>\w+)>\w+</(?P=tag_name)>","<h1>hello</h1>").groupdict()) #<h1>hello</h1>

print(re.search(r"<(\w+)>\w+</(\w+)>","<h1>hello</h1>").group())
print(re.search(r"<(\w+)>\w+</\1>","<h1>hello</h1>").group())
補充一
#補充二
import re

#使用|,先匹配的先生效,|左邊是匹配小數,而findall最終結果是檢視分組,所有即使匹配成功小數也不會存入結果
#而不是小數時,就去匹配(-?\d+),匹配到的自然就是,非小數的數,在此處即整數
#
print(re.findall(r"-?\d+\.\d*|(-?\d+)","1-2*(60+(-40.35/5)-(-4*3))")) #找出所有整數['1', '-2', '60', '', '5', '-4', '3']

#找到所有數字:
print(re.findall('\D?(\-?\d+\.?\d*)',"1-2*(60+(-40.35/5)-(-4*3))")) # ['1','2','60','-40.35','5','-4','3']

#計算器作業參考:http://www.cnblogs.com/wupeiqi/articles/4949995.html
expression='1-2*((60+2*(-3-40.0/5)*(9-2*5/3+7/3*99/4*2998+10*568/14))-(-4*3)/(16-3*2))'

content=re.search('\(([\-\+\*\/]*\d+\.?\d*)+\)',expression).group() #(-3-40.0/5)
#為何同樣的表示式search與findall卻有不同結果:
print(re.search('\(([\+\-\*\/]*\d+\.?\d*)+\)',"1-12*(60+(-40.35/5)-(-4*3))").group()) #(-40.35/5)
print(re.findall('\(([\+\-\*\/]*\d+\.?\d*)+\)',"1-12*(60+(-40.35/5)-(-4*3))")) #['/5', '*3']

#看這個例子:(\d)+相當於(\d)(\d)(\d)(\d)...,是一系列分組
print(re.search('(\d)+','123').group()) #group的作用是將所有組拼接到一起顯示出來
print(re.findall('(\d)+','123')) #findall結果是組內的結果,且是最後一個組的結果
search與findall
#_*_coding:utf-8_*_
__author__ = 'Linhaifeng'
#線上除錯工具:tool.oschina.net/regex/#
import re

s='''
http://www.baidu.com
[email protected]
你好
010-3141
'''

#最常規匹配
# content='Hello 123 456 World_This is a Regex Demo'
# res=re.match('Hello\s\d\d\d\s\d{3}\s\w{10}.*Demo',content)
# print(res)
# print(res.group())
# print(res.span())

#泛匹配
# content='Hello 123 456 World_This is a Regex Demo'
# res=re.match('^Hello.*Demo',content)
# print(res.group())


#匹配目標,獲得指定資料

# content='Hello 123 456 World_This is a Regex Demo'
# res=re.match('^Hello\s(\d+)\s(\d+)\s.*Demo',content)
# print(res.group()) #取所有匹配的內容
# print(res.group(1)) #取匹配的第一個括號內的內容
# print(res.group(2)) #去陪陪的第二個括號內的內容



#貪婪匹配:.*代表匹配儘可能多的字元
# import re
# content='Hello 123 456 World_This is a Regex Demo'
#
# res=re.match('^He.*(\d+).*Demo$',content)
# print(res.group(1)) #只打印6,因為.*會盡可能多的匹配,然後後面跟至少一個數字


#非貪婪匹配:?匹配儘可能少的字元
# import re
# content='Hello 123 456 World_This is a Regex Demo'
#
# res=re.match('^He.*?(\d+).*Demo$',content)
# print(res.group(1)) #只打印6,因為.*會盡可能多的匹配,然後後面跟至少一個數字


#匹配模式:.不能匹配換行符
content='''Hello 123456 World_This
is a Regex Demo
'''
# res=re.match('He.*?(\d+).*?Demo$',content)
# print(res) #輸出None

# res=re.match('He.*?(\d+).*?Demo$',content,re.S) #re.S讓.可以匹配換行符
# print(res)
# print(res.group(1))


#轉義:\

# content='price is $5.00'
# res=re.match('price is $5.00',content)
# print(res)
#
# res=re.match('price is \$5\.00',content)
# print(res)


#總結:儘量精簡,詳細的如下
    # 儘量使用泛匹配模式.*
    # 儘量使用非貪婪模式:.*?
    # 使用括號得到匹配目標:用group(n)去取得結果
    # 有換行符就用re.S:修改模式















#re.search:會掃描整個字串,不會從頭開始,找到第一個匹配的結果就會返回

# import re
# content='Extra strings Hello 123 456 World_This is a Regex Demo Extra strings'
#
# res=re.match('Hello.*?(\d+).*?Demo',content)
# print(res) #輸出結果為None

#
# import re
# content='Extra strings Hello 123 456 World_This is a Regex Demo Extra strings'
#
# res=re.search('Hello.*?(\d+).*?Demo',content) #
# print(res.group(1)) #輸出結果為



#re.search:只要一個結果,匹配演練,
import re
content='''
<tbody>
<tr id="4766303201494371851675" class="even "><td><div class="hd"><span class="num">1</span><div class="rk "><span class="u-icn u-icn-75"></span></div></div></td><td class="rank"><div class="f-cb"><div class="tt"><a href="/song?id=476630320"><img class="rpic" src="http://p1.music.126.net/Wl7T1LBRhZFg0O26nnR2iQ==/19217264230385030.jpg?param=50y50&amp;quality=100"></a><span data-res-id="476630320" "
# res=re.search('<a\shref=.*?<b\stitle="(.*?)".*?b>',content)
# print(res.group(1))


#re.findall:找到符合條件的所有結果
# res=re.findall('<a\shref=.*?<b\stitle="(.*?)".*?b>',content)
# for i in res:
#     print(i)



#re.sub:字串替換
import re
content='Extra strings Hello 123 456 World_This is a Regex Demo Extra strings'

# content=re.sub('\d+','',content)
# print(content)


#用\1取得第一個括號的內容
#用法:將123與456換位置
# import re
# content='Extra strings Hello 123 456 World_This is a Regex Demo Extra strings'
#
# # content=re.sub('(Extra.*?)(\d+)(\s)(\d+)(.*?strings)',r'\1\4\3\2\5',content)
# content=re.sub('(\d+)(\s)(\d+)',r'\3\2\1',content)
# print(content)




# import re
# content='Extra strings Hello 123 456 World_This is a Regex Demo Extra strings'
#
# res=re.search('Extra.*?(\d+).*strings',content)
# print(res.group(1))


# import requests,re
# respone=requests.get('https://book.douban.com/').text

# print(respone)
# print('======'*1000)
# print('======'*1000)
# print('======'*1000)
# print('======'*1000)
# res=re.findall('<li.*?cover.*?href="(.*?)".*?title="(.*?)">.*?more-meta.*?author">(.*?)</span.*?year">(.*?)</span.*?publisher">(.*?)</span.*?</li>',respone,re.S)
# # res=re.findall('<li.*?cover.*?href="(.*?)".*?more-meta.*?author">(.*?)</span.*?year">(.*?)</span.*?publisher">(.*?)</span>.*?</li>',respone,re.S)
#
#
# for i in res:
#     print('%s    %s    %s   %s' %(i[0].strip(),i[1].strip(),i[2].strip(),i[3].strip()))
View Code

作業:

import re

# 1、匹配密碼,密碼必須是由6位數字與字母組成,並且不能是純數字也不能是純字母
# 1.1 知識點:# ?!pattern,表示在沒有配到pattern的字串的前提下,再進行後續的正則表示式匹配,後續匹配仍然從被匹配字串的頭開始

# 1.2 答案:
print(re.search("(?!^[0-9]+$)(?!^[a-zA-Z]+$)^[0-9A-Za-z]{6}$","123asf").group())  # 123asf

# 1.3 解釋:
# 上述正則的意思為:在匹配(?!^[0-9]+$)以及(?!^[a-zA-Z]+$)過後,如果字串成功後在從頭去匹配(?!^[a-zA-Z]+$),最終匹配完。


# 2、匹配密碼,密碼強度:強,必須包含大寫,小寫和數字,和特殊字元(!,@,#,%,&),且大於6位
# 2.1 知識點:# ?=pattern,表示在配到pattern的字串的前提下,再進行後續的正則表示式匹配,後續匹配仍然從被匹配字串的頭開始

# 2.2 答案:
# while True:
#     pwd = input("please your password: ").strip()  # 比如輸入:Aa3@adf123
#     pwd_pattern= re.compile("(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[!@#%&])^([a-zA-Z0-9!@#%&]){6,}$")
#     if pwd_pattern.search(pwd) is None:
#         print("密碼強度不夠")
#     else:
#         break

# 2.3 解釋:
# 上述正則表示式的意思:
#(1)首先是(?=.*[A-Z])匹配,.*表示密碼中可以包含多個字元,[A-Z]代表密碼中需要包含至少一個大寫字母,注意一定不要去掉.*寫成(?=[A-Z]),那樣表示密碼只能由一個字元組成,該字元是大寫字母
#(2)其次是(?=.*[a-z])匹配,同上,確保密碼中必須至少有一個小寫字母
#(3)然後是(?=.*[0-9])匹配,同上,確保密碼中必須至少有一個數字
#(4)然後是(?=.*[!@#%&])匹配,同上,,確保密碼中必須至少有一個特殊符號!@#%&

#(5)最後是^([a-zA-Z0-9!@#%&]){6,}$,確保密碼是由[a-zA-Z0-9!@#%&]字元構成,至少有6位


# 3、匹配email
# print(re.findall("(?:[a-zA-Z0-9]+)@(?:[0-9a-zA-Z]+).com","[email protected] [email protected]"))

# 4、匹配身份證
# your_id=input(">>: ").strip()
# print(re.findall("^([0-9]){17}([0-9]|X)$",your_id)) # 17個數字組成,最後一個字元可以是數字或X

# 5、匹配使用者名稱,包含字母或者數字,且8位
# print(re.findall("^[0-9a-zA-Z]{8}$","egonlinh"))

# 5.1、要求輸入的內容只能是漢字
# name=input('>>: ').strip()
# print(re.search(r'[\u4E00-\u9fa5]+',name))

# 6、取出字串裡的數字
# print(re.findall(r'\d+(?:\.\d+)?', 'sww123kw11.333e2lkd'))

# 7、取出所有負整數
# print(re.findall(r'-\d+', '-12,3,54,-13.11,64,-9'))  # 錯誤答案
# print(re.findall(r'(?!-\d+\.\d+)-\d+', '-12,3,54,-13.11,64,-9'))  # 正確答案

# 8、所有數字
# print(re.findall(r'\-?\d+(?:\.\d+)?', '-12.9,3.92,54.11,64,89,-9,-45.2'))

# 9、所有負數
# print(re.findall(r'\-\d+(?:\.\d+)?', '-12.9,3.92,54.11,64,89,-9,-45.2'))

# 10、所有的非負浮點數
print(re.findall(r'\d+\.\d+', '-12.9,3.92,54.11,64,89,-9,-45.2'))

# 11、
msg = """
中文名 貝拉克·侯賽因·奧巴馬 
外文名 Barack Hussein Obama II 
別名 歐巴馬 
性 別 男
國籍 美國 
民 族 德裔族 
出生地 美國夏威夷州檀香山 
出生日期 1961年8月4日 
職 業政治家、律師、總統 
畢業院校 哥倫比亞大學,哈佛大學 
信 仰新教 
主要成就 1996年伊利諾伊州參議員 
主要成就 美國第56屆、57屆總統 2009年諾貝爾和平獎獲得者 時代週刊年度風雲人物2008、2011 任期內清除本·拉登 
代表作品 《我相信變革》《我父親的夢想》《無畏的希望》 
所屬政黨美國民主黨 
血 型 AB型 
學 院西方學院 
妻 子 米歇爾·拉沃恩·奧巴馬 

"""
#外文名
print(re.findall("外文名 (.*)",msg))

#出生日期
print(re.findall('出生日期 (\d{4})年(\d+)月(\d+)日',msg))

#妻子姓名
print(re.findall('妻 子 (\S+)',msg))
View Code

補充:

推薦閱讀:

https://www.cnblogs.com/linhaifeng/articles/13432794.html

正則表示式中(?:pattern)、(?=pattern)、(?!pattern)、(?<=pattern)和(?<!pattern)

下述表示式都是斷言,不佔用寬度

    前面有,正向後發(?<=exp),放前面;

    後面有,正向先行(?=exp),放後面;

    前面無,反向後發(?<!exp),放前面;

    後面無,反向先行(?!exp),放後面。

例如

re.findall("egon(?=100|N)(?=N)N123","egonN123")  # ['egonN123']

# 位置:   0    1    2    3    4    5    6     7
# 字串:e    g    o    n    N    1    2    3


# 分析:
# 步驟1、正則表示式egon匹配到了字串的位置3

然後連續進行兩次斷言匹配
# 步驟2、(?=100|N)從位置3作為起始匹配位置4的字元是否100或者N
# 步驟3、2成功後,繼續匹配(?=N),因為?=patter不會吃字元,所以此時會重新回到步驟1所在位置3,然後繼續匹配,匹配成功

# 步驟4、從位置3開始匹配N123


# 思考下述輸出結果,為何會不同???:
re.findall("egon(?=100|N)(?=N)N123","egonN123")
re.findall("egon(?=100|N)(?=N)123","egonN123")

介紹

(pattern) : 匹配 pattern 並獲取這一匹配,所獲取的匹配可以從產生的 Matches 集合得到。
(?:pattern) :匹配 pattern 但不獲取匹配結果,也就是說這是一個非獲取匹配,不進行儲存供以後使用。
(?=pattern) :正向預查,在任何匹配 pattern 的字串開始處匹配查詢字串。這是一個非獲取匹配,也就是說,該匹配不需要獲取供以後使用。預查不消耗字元,也就是說,在一個匹配發生後,在最後一次匹配之後立即開始下一次匹配的搜尋,而不是從包含預查的字元之後開始。

共同點

(?:pattern)(?=pattern)都匹配pattern,但不會把pattern結果放到Matches的集合中,即Matcher.group()不會匹配到(?;pattern)與(?=pattern)

區別

    • (?:pattern)匹配得到的結果包含pattern,(?=pattern)則不包含。如:
      對字串:"industry abc"的匹配結果:
      industr(?:y|ies) ---> "industry"
      industr(?=y|ies) ---> "industr"

      是否消耗字元
      (?:pattern)消耗字元,下一字元匹配會從已匹配後的位置開始。
      (?=pattern)不消耗字元,下一字元匹配會從預查之前的位置開始。
      即後者只預查,不移動匹配指標。如: