1. 程式人生 > 其它 >python基礎篇之實用庫

python基礎篇之實用庫

Python 的實用離不開其強大的庫或框架的支援,像正則表示式,檔案模組,命令列模組, Django 框架, sk-learning 工具箱等.這些都讓 Python 能夠非常方便的處理不同領域的問題。

正則表示式

初學 Python,對 Python 的文書處理能力有很深的印象,除了 str 物件自帶的一些方法外,就是正則表示式這個強大的模組了。關於 Python 裡面正則表示式的使用網上有很多博文,如 Python正則式學習筆記等,這裡做一下自己的總結.

基本使用

  • match 方法

    import re
    
    str = 'an example word:cat!!'
    match = re.search(
    r'word:\w\w\w', str) # If語句緊跟來檢驗search()是否成功 if match: print 'found', match.group() ## 找到cat else: print 'did not find'
  • findall 方法

    import re
    
    # 在字串中查詢
    str = 'purple [email protected], blah monkey [email protected] blah dishwasher'
    
    # 找出str中所有的正規的郵箱
    emails =
    re.findall(r'[\w\.-][email protected][\w\.-]+', str) ## ['[email protected]', '[email protected]'] for email in emails: print email # 在檔案中查詢 f = open'test.txt','r') strings = re.findall(r'pattern',f.read())

基本模式

  • 普通字元 a-z,A-Z,0-9
  • . 匹配任何一個單字元,除了換行符\n
  • \w 匹配字母/數字/下劃線,等價於 [a-zA-Z0-9] ,\W\w
    互為補集
  • \b 匹配詞和非詞的分隔界限
  • \s 匹配單個空格字元,等價於 [ \n\r\t\f\v]. \S\s 互為補集
  • \t\n\r 匹配製表符,換行符,回車符
  • \d 匹配數字,等價於 [0-9]
  • ^ $ ^ 匹配開頭, $ 匹配結尾

重複模式

  • + 匹配至少有一個和左邊相同的字元構成的串.eg i+ 匹配 i/ii/iii 等

  • * 匹配 0 個或多個和左邊相同的字元構成的串.eg i* 匹配 /i/ii/iii 等

  • ? 匹配 0 個或一個和左邊相同的字元.eg i?匹配 /i

    注意:
    +*都是儘可能多的匹配字串

特殊符號

  • [] 代表單個字元,這個字元是屬於[]中的元素
  • () () 中匹配的字串構成的一個 group,可以通過 match.group(i) 提取第 i 個組合

課後習題

檔案模組

  • 模組支援

    import os
    import shutil
    
  • 常用函式

    # 獲取檔案列表
    filenames = os.listdir(dir)
    
    # 拼接檔案路徑
    os.path.join(dir,filename)
    
    # 獲取絕對路徑
    os.path.abspath(path)
    
    # 從完整路徑中獲取目錄和檔名
    os.path.dirname(path)
    os.path.basename(path)
    
    # 判斷完整路徑是否存在
    os.path.exists(path)
    
    # 建立目錄
    os.mkdir(dir_path)
    # 建立路徑上的所有資料夾
    os.makedirs(dir_path)   
    
    # 拷貝檔案
    shutil.copy(src,dest)
    

命令列模組

  • 模組支援

    import commands
    
  • 常用函式

    # 執行命令,等待結束。返回狀態(正常返回是0)和結果。
    (status, output) = commands.getstatusoutput(cmd)
    
    # 不返回狀態的執行命令
    output = commands.getoutput(cmd)
    
    # 不關心結果,只要執行即可
    os.system(cmd)
    
  • 樣例程式碼

    # 給定一個dir路徑,執行 'ls -l'
    def listdir(dir):
        cmd = 'ls -l ' + dir
        print "Command to run:", cmd   ## 易於除錯
        (status, output) = commands.getstatusoutput(cmd)
        if status:    ## 錯誤情況,列印錯誤資訊並退出
            sys.stderr.write(output)
            sys.exit(1)
        print output  ## 否則顯示輸出結果
    

異常處理

  • 樣例程式碼

    try:
        # 開啟檔案或讀取檔案出現問題將丟擲 IOError 異常.
        f = open(filename, 'rU')
            text = f.read()
            f.close()
    except IOError:
        # 處理 IOError 異常.
        sys.stderr.write('problem reading:' + filename)
    # try/except後面的程式碼將繼續執行
    

網路模組

  • 模組支援

    import urllib
    
  • 常用函式

    # 返回一個檔案
    ufile = urllib.urlopen(url)
    
    # 讀取檔案資訊
    text = ufile.read()
    
    # 獲取網路檔案meta/type 資訊
    info = ufile.info()
    type = info.gettype()
    
    # 獲取最初始的url(針對重定向)
    baseurl = ufile.geturl()
    
    # 下載url資料到指定的檔案
    urllib.urlretrieve(url,filename)
    
    # 獲取完整url
    urlparse.urljoin(baseurl,url)
    
  • 樣例程式碼

    ## 用try/except處理網路異常
    def wget(url):
        try:
            ufile = urllib.urlopen(url)
            if ufile.info().gettype() == 'text/html':
                print ufile.read()
        except IOError:
            print 'problem reading url:', url
    
    wget('http://www.baidu.com')  # 一定要加http://
    

課後練習


如果該文章對您產生了幫助,或者您對技術文章感興趣,可以關注微信公眾號: 技術茶話會, 能夠第一時間收到相關的技術文章,謝謝!

技術茶話會

本篇文章由一文多發平臺ArtiPub自動釋出