Python标准库中一些模块

来源:互联网 发布:sql insert into 语法 编辑:程序博客网 时间:2024/05/17 01:07
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数据库
0 0
原创粉丝点击