python怎么解压压缩的字符串数据
2013-07-28 18:22 江湖么名 阅读(13074) 评论(0) 编辑 收藏 举报范例1: gzip
import StringIO import gzip compresseddata = gzip方式压缩的字符串(html) compressedstream = StringIO.StringIO(compresseddata) gzipper = gzip.GzipFile(fileobj=compressedstream) data = gzipper.read() # data就是解压后的数据
一个简单的例子
1 import urllib2 2 from StringIO import StringIO 3 import gzip 4 5 def loadData(url): 6 request = urllib2.Request(url) 7 request.add_header('Accept-encoding', 'gzip') 8 response = urllib2.urlopen(request) 9 if response.info().get('Content-Encoding') == 'gzip': 10 print 'gzip enabled' 11 buf = StringIO(response.read()) 12 f = gzip.GzipFile(fileobj=buf) 13 data = f.read() 14 else: 15 data = response.read() 16 return data