TypeError: string argument expected, got 'bytes'

TypeError: string argument expected, got 'bytes'

我想将下面的十六进制序列转换为图像,在筛选过程中筛选了很多与我类似的问题 none 已经接近 [=11= 中解决的问题], 我的代码在下面,我哪里出错了?

data = "2a2b2c2a2b2c2a2b2c2a2b2cb1"
buf = io.StringIO()    
for line in data.splitlines():
    line = line.strip().replace(" ", "")
    if not line:
        continue
    bytez = binascii.unhexlify(line)
    buf.write(bytez)

with open("image.jpg", "wb") as f:
    f.write(buf.getvalue()) 

io.StringIO() 创建一个产生文本流的字符串对象。 您需要 io.BytesIO(),它会创建一个字节对象,您可以将二进制数据写入其中:

buf = io.BytesIO()

...

buf.write(bytez)

另见 io — Core tools for working with streams