python gzip解压_Python解压缩内存中的gzip数据而无需文件

我从HTTP回复中压缩了数据.我有以下代码:

def gzipDecode(self, content):

import StringIO

import gzip

outFilePath = 'test'

compressedFile = StringIO.StringIO(content)

decompressedFile = gzip.GzipFile(fileobj=compressedFile)

with open(outFilePath, 'w') as outfile:

outfile.write(decompressedFile.read())

data = ''

with open(outFilePath, 'r') as myfile:

data=myfile.read().replace('

', '')

return data

解压缩输入的压缩内容并返回字符串(http答复是json压缩). – 有用.

但是我需要它而不创建测试文件-全部都在内存中.

我将其修改为:

def gzipDecode(self, content):

import StringIO

from io import BytesIO

import gzip

outFile = StringIO.StringIO()

compressedFile = StringIO.StringIO(content)

decompressedFile = gzip.GzipFile(fileobj=compressedFile)

outFile.write(decompressedFile.read())

outFile.flush()

data = outFile.read().replace('

', '')

print "_" + data + "_"

return data

但在解析json时崩溃(gzipDecode产生空输出):

Traceback (most recent call last):

__

File "/usr/lib/python2.7/SocketServer.py", line 593, in process_request_thread

self.finish_request(request, client_address)

----------------------------------------

File "/usr/lib/python2.7/SocketServer.py", line 334, in finish_request

Exception happened during processing of request from ('10.123.66.3', 39853)

self.RequestHandlerClass(request, client_address, self)

----------------------------------------

File "/usr/lib/python2.7/SocketServer.py", line 649, in __init__

self.handle()

File "/usr/lib/python2.7/BaseHTTPServer.py", line 340, in handle

self.handle_one_request()

File "/usr/lib/python2.7/BaseHTTPServer.py", line 328, in handle_one_request

method()

File "/tmp/test_server.py", line 92, in do_POST

data = json.loads(file_content)

File "/usr/lib/python2.7/json/__init__.py", line 338, in loads

return _default_decoder.decode(s)

File "/usr/lib/python2.7/json/decoder.py", line 366, in decode

obj, end = self.raw_decode(s, idx=_w(s, 0).end())

File "/usr/lib/python2.7/json/decoder.py", line 384, in raw_decode

raise ValueError("No JSON object could be decoded")

ValueError: No JSON object could be decoded

我做什么不好?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值