1. 程式人生 > >Python標準庫中一些模組

Python標準庫中一些模組

import re:呼叫正則表示式
提供高階字串模式匹配方案。該模組中包含的關鍵函式有:
compile()--將一個RE表示式編譯成一個可重用的RE物件
match()--試圖從字串的開始匹配一個模式
search()--找出字串中所有匹配的項
sub()--進行查詢替換操作

import string:引用字串

from string import Template:呼叫Template,將字串格式固定
Template物件有兩個方法:substitute()和safe_substitute()
前者更為嚴謹,key缺少時會報一個KeyError異常,後者缺少key時,直接原封


不動顯示字串。
**************************************************
其主要實現方式為$xxx,其中字串xxx不能以數字開頭
如果$xxx需要和其他字串接觸,可用{}將xxx包裹起來

**************************************************

>>> from string import Template                         #呼叫Template
>>> s = Template('There are ${howmany} ${lang} Quotation Symbols')
>>> print s.substitute(lang = 'Python',howmany = 3)
There are 3 Python Quotation Symbols
>>> print s.substitute(lang = 'Python')
<pre name="code" class="python">Traceback (most recent call last):                      #報錯
  File "<pyshell#146>", line 1, in <module>
    print s.substitute(lang = 'Python')
  File "D:\Python27\lib\string.py", line 172, in substitute
    return self.pattern.sub(convert, self.template)
  File "D:\Python27\lib\string.py", line 162, in convert
    val = mapping[named]
KeyError: 'howmany'
>>> print s.safe_substitute(lang = 'Python')
There are ${howmany} Python Quotation Symbols
>>> 




struct:字串和二進位制之間的轉換


codecs:解碼器註冊和基類


crypt:進行單方面加密


difflib:找出序列間的不同


hashlib:多種不同安全雜湊演算法和資訊摘要演算法的API


hma:HMAC資訊及安全演算法的Python實現


md5:RSA的MD5資訊摘要鑑權


rotor:提供多平臺的加解密服務


sha:NIAT的安全雜湊演算法SHA


stringprep:提供用於IP協議的Unicode字串


textwrap:文字包裝和填充


unicodedata:Unicode資料庫