PyMoTW介绍python中的常用模块,此文章为PyMoTW下的python3的常用模块学习笔记。
1. Text
string
- string.capwords(s, sep=None)
对于s的每个单词首字母大写,eg:
The quick brown fox jumped over the lazy dog.
The Quick Brown Fox Jumped Over The Lazy Dog. - Templates
用法:t = string.Template('$who is ${what}'), t.safe_substitute(who='someone',what='something'), $为delimiter(可以修改), {}在不引起歧义时可以省略({staw/blue}berry这种情况就不能省略了),推荐用safe_substitute进行替代,以免出错。 - Constants
ascii_letters='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW XYZ' ascii_lowercase='abcdefghijklmnopqrstuvwxyz' ascii_uppercase='ABCDEFGHIJKLMNOPQRSTUVWXYZ' digits='0123456789' hexdigits='0123456789abcdefABCDEF' octdigits='01234567' printable='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQ RSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c' punctuation='!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~' whitespace=' \t\n\r\x0b\x0c'
textwrap(文本包装和填充,一般用不到)
- textwrap.fill(sample_text, width=50), 控制输出宽度,结果为左对齐,第一行有缩进。行中的空格继续保留。
- textwrap.dedent(sample_text),移除缩进。
- 。。。。
re — Regular Expressions(正则表达式,极为重要的功能
)
由于内容较多,先pass
difflib — 比较序列差异
使用方法:
import difflib
from difflib_data import *
d = difflib.Differ()
diff = d.compare(text1_lines, text2_lines)
print('\n'.join(diff))