一,模塊的基本介紹
1,import引入其他標準模塊
標準庫:Python標準安裝包裏的模塊。
引入模塊的幾種方式:
i)引入模塊:import moduleName
ii)引入模塊下的函數:from moduleName import function1,function2,……
iii)引入模塊下的所有函數:from moduleName import *
使用模塊裏的函數的方法:
moduleName.function(agrs)
示例:
>>> import math >>> r=math.sqrt(16) >>> print r 4.0
如果模塊或者函數名字過長可以在import後使用as給該模塊取別名,之後可以通過“別名.函數”使用模塊裏的函數。
示例:
>>> import webbrowser as web >>> web.open_new_tab('http://www.cnblogs.com') True
2,使用自定義模塊
testfile.py下:
def readfile(): fr=open('wformat.txt','r') while (1==1): line=fr.readline() if(line==''): break else: print line fr.close()
test.py下:(與testfile.py同在一個目錄文件裏面)
import testfile testfile.readfile()
結果如圖:
>>> name age sex 張三 78 male 李四 50 male 王五 80 male 張強 90 female
調用層次結構:
如果被調用模塊程序與模塊程序不在同一個目錄文件下,則需要調用os.path.append(模塊文件所在的目錄)
注意:.pyc是模塊字節碼文件,在使用模塊是Python解釋器需要把模塊加載到系統裏,若發現.py比.pyc新,則需要重新編譯生成.pyc,否則不需要。文件.pyc是在第一次加載模塊文件時完成的。
3,使用模塊示例Json模塊
1)Python下使用dumps函數可以將Python數據字典轉換成Json數據結構。
示例:
import json s=[{'f':(1,3,5,'a'),'x':None}] d=json.dumps(s) print d
結果如圖:
>>> [{"x": null, "f": [1, 3, 5, "a"]}]
2)Json數據轉換Python數據,解碼loads(對應關係如上表)
二,正則模塊re
1)正則表達式的常見應用:
驗證字符串:如合法的郵件地址,HTTP地址等。
查找字符串
替換字符串
提取字符串
2)基本概念:
正則表達式:是一段文本或者一個公式,用來描述用某種模式去匹配一類字符串的公式,並且該公式具有一定的模式。
匹配:給定一段文本或者字符串,使用正則表達式從文本或字符串中查找出符合正則表達式的字符串。有可能文本或字符串存在不止一個部分滿足給定的正則表達式,這是每一個這樣的部分被稱爲一個匹配。
元字符:一次只能匹配一個字符或者一個位置。故元字符分:匹配字符的元字符和匹配位置的元字符。
i)匹配字符的元字符
^string:匹配所有以string字符串開始的行
string$:匹配所有以string字符串結尾的行
$^:匹配空行
\bStr:\b匹配以Str開始的單詞(等價於\<)
Str\b:\b匹配以Str結尾的單詞(等價於\>)
ii)匹配位置的元字符
\w:匹配單詞裏字符(字母,數字和下劃線)
\W:匹配單詞裏非字符
\d:匹配單詞裏數字
\D:匹配單詞裏非數字
\s:匹配單詞裏空格字符
\S:匹配單詞裏非空格字符
示例:
>>> import re >>> s='Hello www.jeapedu.com' >>> res=r'\bjea' >>> print re.findall(res,s) ['jea'] >>> res=r'jea\b' >>> print re.findall(res,s) [] >>> res=r'edu\b' >>> print re.findall(res,s) ['edu'] import re s=''' ahello www.baidu.com hello world nice hellod world piece of helloe world ''' res=r'\hello' print 'hello:',re.findall(res,s) >>> ================================ RESTART ================================ >>> hello ['hello', 'hello', 'hello', 'hello'] import re s='a1b2c3d' res='\w\d' print re.findall(res,s) res='\w\d\w' print re.findall(res,s) >>> ================================ RESTART ================================ >>> ['a1', 'b2', 'c3'] ['a1b', 'c3d']
字符集:用方括號括起來的字符集合,如果其中的任何一個字符被匹配,則它就會找到該匹配項,反字符集可在字符前加^。
示例:
import re s='''Plz write a mail to [email protected] or [email protected],thanks!''' res=r'\w[\w\.-]+@[\w\.-]+\.\w{2,4}' #*表示匹配0次及其以上,+表示匹配1次及以上。 print re.findall(res,s) >>> ================================ RESTART ================================ >>> ['[email protected]', '[email protected]']
分組或子模式:把一個正則表達式的全部或者部分分成一個或多個組。其中,分組使用的字符是“(”和“)”。
示例:
import re s='''www.baidu.comwww.BaidU.comwww.bAIDU.comwww.baidu.comwww.Baidu.com''' res1=r'(b|B)\w*(u|U)' #*表示匹配0次及其以上,+表示匹配1次及以上。 res2=r'[bB]\w*(u|U)' res3=r'[bB]\w*[uU]' print res1+':' print re.findall(res1,s) print res2+':' print re.findall(res2,s) print res3+':' print re.findall(res3,s) >>> ================================ RESTART ================================ >>> (b|B)\w*(u|U): [('b', 'u'), ('B', 'U'), ('b', 'U'), ('b', 'u'), ('B', 'u')] [bB]\w*(u|U): ['u', 'U', 'U', 'u', 'u'] [bB]\w*[uU]: ['baidu', 'BaidU', 'bAIDU', 'baidu', 'Baidu']
限定符:用於指定允許特定字符或字符集自身重複出現的次數。
(pattern)?:重複0次或者1次,等價於{0,1}
(pattern)*:至少重複0次,等價於{0,}
(pattern)+:至少重複1次,等價於{1,}
(pattern){m:n}:重複至少m次,至多m次
(pattern)??:使用重複0次或者1次
(pattern)*?:儘可能少地使用重複的第一個匹配
(pattern)+?:儘可能少地使用重複但至少使用一次
示例:
import re s='''Tell to me 110-123-1114119 or call 4008-6688-9988 or 13306247965''' res=r'\d+\D\d+\D\d+' #*表示匹配0次及其以上,+表示匹配1次及以上。 res1=r'\d{11}' print re.findall(res,s) print re.findall(res1,s) >>> ================================ RESTART ================================ >>> ['110-123-1114119', '4008-6688-9988'] ['13306247965']
通配符:匹配限定長度的字符串,例如點號匹配任意一個字符。
轉義字符:(詳見第三章)
3)應用舉例:
import re s='hello www.baidu.com' print '----------------compile--------------------' res='[\s\.]' pat=re.compile(res) print pat.findall(s) print pat.split(s) print '----------------split--------------------' res='[\s\.]' print re.findall(res,s) print re.split(res,s) >>> ================================ RESTART ================================ >>> ----------------compile-------------------- [' ', '.', '.'] ['hello', 'www', 'baidu', 'com'] ----------------split-------------------- [' ', '.', '.'] ['hello', 'www', 'baidu', 'com']
三、小結
本章主要介紹python開發的進階知識,模塊及其正則的相關知識,正則表達式是編程的一個很重要的知識點,需多多研究。
額外補充一點小知識:
1,sys模塊:包含了與Python的解釋器和它的環境有關的函數。
import sys for i in sys.argv: print i >>> D:\Python學習\pythontest\test.py
2,dir函數:列舉模塊定義的標識符,如函數、類和變量。當爲dir提供一個模塊名稱的時候,返回模塊定義的名稱列表,否則返回當前模塊中定義的名稱列表。
>>> import sys >>> dir() ['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'i', 'sys'] >>> a=5 >>> dir() ['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'a', 'i', 'sys'] >>> del a #刪除一個變量/名稱 >>> dir() ['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'i', 'sys']