Python模块学习
Python标准模块中,有多个模块用于数据的压缩与解压缩,如zipfile,gzip, bz2等等。上次介绍了zipfile模块,今天就来讲讲zlib模块。 zlib.compress(string[, level]) zlib.decompress(string[, wbits[, bufsize]]) zlib.compress用于压缩流数据。参数string指定了要压缩的数据流,参数level指定了压缩的级别,它的取值范围是1到9。压缩速度与压缩率成反比,1表示压缩速度最快,而压缩率最低,而9则表示压缩速度最慢但压缩率最高。zlib.decompress用于解压数据。参数string指定了需要解压的数据,wbits和bufsize分别用于设置系统缓冲区大小(window buffer )与输出缓冲区大小(output buffer)。下面用一个例子来演示如何使用这两个方法: #coding=gbk
import zlib, urllib
fp = urllib.urlopen('http://localhost/default.html')
str = fp.read()
fp.close()
#---- 压缩数据流。
str1 = zlib.compress(str, zlib.Z_BEST_COMPRESSION)
str2 = zlib.decompress(str1)
print len(str)
print len(str1)
print len(str2) # ---- 结果
#5783
#5783 我们也可以使用Compress/Decompress对象来对数据进行压缩/解压缩。zlib.compressobj([level]) 与zlib.decompress(string[, wbits[, bufsize]]) 分别创建Compress/Decompress缩对象。通过对象对数据进行压缩和解压缩的使用方式与上面介绍的zlib.compress,zlib.decompress非常类似。但两者对数据的压缩还是有区别的,这主要体现在对大量数据进行操作的情况下。假如现在要压缩一个非常大的数据文件(上百M),如果使用zlib.compress来压缩的话,必须先一次性将文件里的数据读到内存里,然后将数据进行压缩。这样势必会战用太多的内存。如果使用对象来进行压缩,那么没有必要一次性读取文件的所有数据,可以先读一部分数据到内存里进行压缩,压缩完后写入文件,然后再读其他部分的数据压缩,如此循环重复,只到压缩完整个文件。下面一个例子来演示这之间的区别:
#coding=gbk
import zlib, urllib
fp = urllib.urlopen('http://localhost/default.html') # 访问的到的网址。
data = fp.read()
fp.close()
#---- 压缩数据流
str1 = zlib.compress(data, zlib.Z_BEST_COMPRESSION)
str2 = zlib.decompress(str1)
print '原始数据长度:', len(data)
print '-' * 30
print 'zlib.compress压缩后:', len(str1)
print 'zlib.decompress解压后:', len(str2)
print '-' * 30
相关文档:
"""A parser for HTML and XHTML."""
# This file is based on sgmllib.py, but the API is slightly different.
# XXX There should be a way to distinguish between PCDATA (parsed
# character data -- the normal case), RCDATA (replaceable character
# data -- only char and entity references and end tags a ......
from http://blog.alexa-pro.cn/?p=315
此文档使用平台为 cPAMIE Build 2.0,和之前的版本有明显的差别,具体可直接看cPAMIE.py 源码
下面是一些常用的方法
ie.navigate('http://blog.alexa.cn') 用来访问一个链接。
ie.linkClick('linkname') 打开这个页面中的一个连接 参数: name或 id
ie.textBoxSet('labels','python ......
from http://stackoverflow.com/questions/101268/hidden-features-of-python
http://www.okpython.com/bbs/thread-3434-1-1.html
http://www.okpython.com/bbs/thread-3436-1-2.html
http://www.okpython.com/bbs/thread-3437-1-2.html
http://www.okpython.com/bbs/thread-3438-1-1.html
http://www.okpython.com/bb ......
安装文件下载:
MySQL-python-1.2.2.win32-py2.6.exe
https://docs.google.com/leaf?id=0B-C0ABoe2nuLMDlhZjI5OTQtMWNmYy00ZTNlLWJjNzMtYTc2Y2EzMGFjMzcy&hl=zh_CN
需要的额外dll文件下载:
libmmd.dll
https://docs.google.com/leaf?id=0B-C0ABoe2nuLZTk2M2RiZTAtYzY5My00NzNjLTg5ZWEtMzRkZGEyYjUxNThh&hl=zh_C ......