简体   繁体   中英

split python bytearray character by character

So, I've got some python code

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

which I adapted from 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. I'm using a bytearray because I'm actually working with sockets and for a given large message I want to accumulate them.

    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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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