以下模块直接支持通用的数据打包和压缩格式:zlib,gzip,bz2,zipfile以及tarfile: import zlib s = b'witch which has which witches wrist watch' len(s) 41 t = zlib.compress(s) len(t) 37 zlib.decompress(t ...
math模块为浮点运算提供了对底层 C 函数库的访问: import math math.cos(math.pi / 4.0) 0.70710678118654757 math.log(1024, 2) 10.0 random提供了生成随机数的工具: import random random.choice() 'apple' ra ...
re模块为高级字符串处理提供了正则表达式工具。对于复杂的匹配和处理,正则表达式提供了简洁、优化的解决方案: import re re.findall(r'\bf*', 'which foot or hand fell fastest') re.sub(r'(\b+) \1', r'\1', ' ...
sys还有stdin,stdout和stderr属性,即使在stdout被重定向时,后者也可以用于显示警告和错误信息: sys.stderr.write('Warning, log file not found starting a new one\n') Warning, log file not found starting a ...