【问题标题】:Python zlib module error when decompressing?解压时Python zlib模块错误?
【发布时间】:2019-09-14 05:01:41
【问题描述】:

几天前我写了一个python程序来压缩一些Html数据并插入到数据库中。我使用 zlib 压缩它们。

html = "<html><head><title>Title</title></head><body><p>Paragraph</p></body></html>"
compressed_html = str(zlib.compress(html.encode('utf-8'))).replace('b\'', '').replace('\'', '')

那么compressed_html 变量类似于,

x\\x9c\\xd4\\xbd\\xfbv\\xdb\\xb6\\xb30\\xfa\\x7f\\xd6\\xfa\\xde...

今天我试着像这样解压缩它们。

html = html.encode('utf-8')
# html is retrieved from database.
# html is like now b'x\\x9c\\xd4\\xbd\\xfbv\\xdb\\xb6\\xb30\\xfa\\x7f\\xd6\\xfa\\xde...'
decompressed = zlib.decompress(html)

这会引发错误:

Traceback(最近一次调用最后一次):

文件“C:/Users/Sakith Karunasena/PycharmProjects/Twibot Repairer/main.py”,第 16 行,

解压 = zlib.decompress(html)

zlib.error: 解压数据时出现错误 -3: 错误的标头检查

【问题讨论】:

  • 你必须放回你删除的 b\' 和 \' - 然后你就会有可以解压缩的数据。
  • @furas 怎么样?请举例。
  • 坦率地说,你不能把它放回去——你不应该删除它们。
  • 您不能只删除部分压缩数据并期望它解压缩。如果您无法重新创建这些部分的位置并将它们放回原处,则您的数据已损坏。下次测试压缩和解压缩,然后再依靠它来保存数据几天。

标签: python python-3.x compression zlib


【解决方案1】:

要压缩它使用这个

compressed_html = zlib.compress(html.encode(), level=6)

级别可以在 -1 到 9 之间,具体取决于所需的压缩比 here

存储它:

with open('filename.txt','wb') as outfile:
    outfile.write(compressed_html)

阅读

with open('filename.txt','rb') as infile:
    compressed_html = infile.read()

解压回来

decompressed_html = zlib.decompress(compressed_html).decode()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-21
    • 1970-01-01
    • 2014-01-22
    • 1970-01-01
    相关资源
    最近更新 更多