简体   繁体   English

逐个字符拆分 python bytearray

[英]split python bytearray character by character

So, I've got some python code所以,我有一些 python 代码

string = "Python is interesting."
arr = bytearray(string, 'utf-8')
arr.split(b'\'')

which I adapted from Split String by char Character我改编自Split String by char Character

but it returns a但它返回一个

[bytearray(b'Python is interesting.')]

I was hoping to split it byte-by-byte.我希望逐字节拆分它。 The end goal is to pass this to a C++ boost wrapper which expects an array of bytes character-by-character.最终目标是将其传递给 C++ 提升包装器,该包装器需要一个字节数组。 I'm using a bytearray because I'm actually working with sockets and for a given large message I want to accumulate them.我正在使用字节数组,因为我实际上正在使用 sockets 并且对于给定的大消息,我想累积它们。

    expected_length = _get_obj_size(socket)
    running_length = 0
    accumulator = bytearray(b'')

    while running_length < expected_length:
        msg = socket.recv(BUFFER_SIZE)
        if msg:
            running_length += len(msg)
            accumulator += msg
        logger.debug("Finished reading in payload")

^ the above isn't particularly relevant but I felt like it might be useful to show why I'm doing what I'm doing. ^ 以上内容并不特别相关,但我觉得展示我为什么要做我正在做的事情可能很有用。

Taking the hint from @Epsi95 what about:从@Epsi95 那里得到提示:

[chr(x) for x in list(arr)]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM