以下模块直接支持通用的数据打包和压缩格式:zlib,gzip,bz2,lzma,zipfile以及tarfile。 import zlib s = b'witch which has which witches wrist watch' len(s) 41 t = zlib.compress(s) len(t) 37 zlib.de ...
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 ...
os模块提供了很多与操作系统交互的函数: import os os.getcwd() # Return the current working directory 'C:\\Python35' os.chdir('/server/accesslogs') # Change current working directory os.system('mkdir ...
现在你可能注意到大多数容器对象都可以用for遍历:for element in : print(element) for element in (1, 2, 3): print(element) for key in {'one':1, 'two':2}: print(key) for char in "123": print(char) for line ...