zlib TypeError:需要一个类似字节的对象,而不是'str'

时间:2018-07-15 12:32:48

标签: python python-3.x zlib

我使用此代码来编码和压缩文本。但这不能正常工作:

Traceback (most recent call last): File "E:\SOUND.py", line 114, in <module>
unhexsring = str(zlib.decompress(unhexsring).encode('utf8'))
TypeError: a bytes-like object is required, not 'str' 

你能帮我吗?

import zlib,gzip

def str2hex(s):
    return binascii.hexlify(bytes(str.encode(s)))


def hex2str(h):
    return binascii.unhexlify(h)

hexstring = input()
if len(hexstring) > 200:
    hexstring = str(zlib.compress(hexstring.encode('utf-8')))
    print(hexstring)
hexstring = str2hex(hexstring)
ph = str(hexstring.decode('utf-8'))
print(ph)

#decompressing text
unhexsring = hex2str(hexstring).decode('utf8')
if 'x' in str(unhexsring):
    print('compressed')
    unhexsring = str(zlib.decompress(unhexsring).encode('utf8'))
print(unhexsring)

此代码不会解压缩zlib压缩的文本。

因此编码工作良好。

我的麻烦是,当我获得编码的字符串并对其进行压缩时,我无法对其进行解压缩。 应该如何工作:

1>s = input('some text')
2>if len(s) > 200: s = str(zlib.compress(s.encode('utf-8'))) 
3>encoding it with str2hex()
4>decode it with hex2str()
5>str(zlib.decompress(unhexs).encode('utf8'))  <---------- HERE

我无法正确解压缩,因为得到了这个:

下一个控制台转储

Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) [MSC v.1914 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> 
========================= RESTART: E:\SOUND.py =========================
dghlkdushfgkjdsfhglkjhsdfgjhdskfjhgkdsfhgkjdhfgkjsdhfgjkhsdkjfghlkjsdhgkjhsdfjghdksjhgkjsdhgkjhsdfkjghdskfjghkdjghdghlkdushfgkjdsfhglkjhsdfgjhdskfjhgkdsfhgkjdhfgkjsdhfgjkhsdkjfghlkjsdhgkjhsdfjghdksjhgkjsdhgkjhsdfkjghdskfjghkdjghdghlkdushfgkjdsfhglkjhsdfgjhdskfjhgkdsfhgkjdhfgkjsdhfgjkhsdkjfghlkjsdhgkjhsdfjghdksjhgkjsdhgkjhsdfkjghdskfjghkdjghdghlkdushfgkjdsfhglkjhsdfgjhdskfjhgkdsfhgkjdhfgkjsdhfgjkhsdkjfghlkjsdhgkjhsdfjghdksjhgkjsdhgkjhsdfkjghdskfjghkdjgh
b'x\x9c\xed\x8d\xb1\r\xc0@\x08\x03\x97\xb5\xb0e\x7f\x87\xb2\x7f\x9eO\x93\x05\xd2\xa5\x02\x1d>\x0cj\x05W\xab\x18\xa3K\\\xb1\x1aE\x0b\x9d\xb2\x98\x83\xf7\xf5dz\x86\xb3#q\x8d<\x84\x8fc\n\xe9Q^0C\xe7\x13\x15\xcc\xfe7~\xd0x\x03\x88\x05\xbb\x9d'
6227785c7839635c7865645c7838645c7862315c725c786330405c7830385c7830335c7839375c7862355c786230655c7837665c7838375c7862325c7837665c7839654f5c7839335c7830355c7864325c7861355c7830325c7831643e5c7830636a5c783035575c7861625c7831385c7861334b5c5c5c7862315c783161455c7830625c7839645c7862325c7839385c7838335c7866375c786635647a5c7838365c78623323715c7838643c5c7838345c783866635c6e5c786539515e30435c7865375c7831335c7831355c7863635c786665377e5c786430785c7830335c7838385c7830355c7862625c78396427
compressed
Traceback (most recent call last):
  File "E:\SOUND.py", line 114, in <module>
    unhexsring = str(zlib.decompress(unhexsring).encode('utf8'))
TypeError: a bytes-like object is required, not 'str'

2 个答案:

答案 0 :(得分:5)

您在这里看到的异常:

unhexsring = str(zlib.decompress(unhexsring).encode('utf8'))
    TypeError: a bytes-like object is required, not 'str'
之所以提出

是因为zlib.decompress期望bytes。更改即可轻松解决

unhexsring = hex2str(hexstring).decode('utf8')    # -> str

unhexsring = hex2str(hexstring)    # -> bytes

但这会导致新的错误:

unhexsring = zlib.decompress(unhexsring)
    zlib.error: Error -3 while decompressing data: incorrect header check

发生这一行是因为此行:

hexstring = str(zlib.compress(hexstring.encode('utf-8')))

str实例上调用bytes不会将bytes转换为str,而是将字节的repr转换为str

>>> bs = 'Hello World'.encode('utf-8')
>>> print(repr(bs))
b'Hello World'
>>> s = str(bs)
>>> print(repr(s))
"b'Hello World'"    # <- note the b....

str转换在压缩数据的前面插入了一个“ b”,从而破坏了标题。现在让我们将十六进制字符串保留为字节对象

hexstring = zlib.compress(hexstring.encode('utf-8'))

但是现在代码引发了另一个异常:

return binascii.hexlify(bytes(str.encode(s)))
    TypeError: descriptor 'encode' requires a 'str' object but received a 'bytes'

s现在是一个bytes对象,因此无需尝试对其进行转换(请注意,str.encode无论如何都会返回bytes,因此bytes即使s是字符串,调用也将是多余的。

所以str2hex变成

def str2hex(s):
    return binascii.hexlify(s)

现在又引发了另一个错误:

unhexsring = str(zlib.decompress(unhexsring).encode('utf8'))
    AttributeError: 'bytes' object has no attribute 'encode'

zlib.decompress的输出是一个bytes对象,因此它已经被编码(假设它是一个字符串开头)。您想对其进行解码以获得str

unhexsring = zlib.decompress(unhexsring).decode('utf8')

这是您的代码的一个版本,可以从命令提示符处作为脚本运行:

import binascii
import random
import string
import zlib,gzip

import zlib,gzip

def str2hex(s):
    return binascii.hexlify(s)


def hex2str(h):
    return binascii.unhexlify(h)

def main():
    # I don't want to type 200+ chars to test this :-)
    hexstring = ''.join(random.choice(string.ascii_letters) for _ in range(201))
    if len(hexstring) > 200:
        hexstring = zlib.compress(hexstring.encode('utf-8'))
        print(hexstring)
    hexstring = str2hex(hexstring)
    ph = str(hexstring.decode('utf-8'))
    print(ph)

    #decompressing text
    unhexsring = hex2str(hexstring)
    if 'x' in str(unhexsring):
        print('compressed')
        unhexsring = zlib.decompress(unhexsring).decode('utf8')
    print(unhexsring)

if __name__ == '__main__':
    main()

答案 1 :(得分:0)

一个非常有趣的问题,我认为您应该摆脱这个问题

hexstring = input()

带有代码的简单示例

>>> s ='Vladimir'
>>> str.encode(s)
b'Vladimir'
>>> bytes(str.encode(s))
b'Vladimir'
>>> binascii.hexlify(bytes(str.encode(s)))
b'566c6164696d6972'
>>> binascii.unhexlify(b'566c6164696d6972')
b'Vladimir'
>>> import zlib,gzip
>>> hexstring = str(zlib.compress(s.encode('utf-8')))
>>> type (hexstring)
<class 'str'>
>>> type (s)
<class 'str'>