将代码转换为python3,其中str和bytes不能连在一起

2024-09-19 23:56:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试图将这段代码转换为在Python3中使用,但我不知道如何使它在int和byte对象之间不再存在cancat的情况下工作。 编辑:这可以工作,但会导致一个新的错误在这里它链接到完整的文件:https://github.com/ndye/tiboyce/blob/master/conv_skin.py

主要的错误是现在的数据都在内存中,等待写入它是给我这个错误

Traceback (most recent call last):
  File "conv_skin2.py", line 42, in <module>
    to_append = ',' * comma + '$%02X' % ord(byte)
TypeError: ord() expected string of length 1, but int found
def compress(data):
    return chr(len(data) % 256) + chr(len(data) / 256) \
        + lzf.compress(data)

Tags: 对象代码pydatalen错误bytecompress
1条回答
网友
1楼 · 发布于 2024-09-19 23:56:44

您只需在串联之前将字符串转换为字节:

def compress(data):
    return (chr(len(data) % 256) + chr(len(data) / 256)).encode('latin1') \
        + lzf.compress(data)

How to cast a string to bytes without encoding

相关问题 更多 >