Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
逐字符分割python字节数组_Python_Sockets_Serialization - Fatal编程技术网

逐字符分割python字节数组

逐字符分割python字节数组,python,sockets,serialization,Python,Sockets,Serialization,所以,我有一些python代码 string = "Python is interesting." arr = bytearray(string, 'utf-8') arr.split(b'\'') 我从中改编的 但它返回一个 [bytearray(b'Python is interesting.')] 我希望把它一个字节一个字节地分开。最终的目标是将此传递给一个C++的Boost包装器,它期望按字符逐个字节数组。我使用bytearray是因为我实际上使用的是套接字,对于

所以,我有一些python代码

string = "Python is interesting."
arr = bytearray(string, 'utf-8')
arr.split(b'\'')
我从中改编的

但它返回一个

[bytearray(b'Python is interesting.')]
我希望把它一个字节一个字节地分开。最终的目标是将此传递给一个C++的Boost包装器,它期望按字符逐个字节数组。我使用bytearray是因为我实际上使用的是套接字,对于给定的大消息,我希望积累它们

    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")
expected_length=_get_obj_size(套接字)
运行长度=0
累加器=字节数组(b'')
运行长度<预期长度时:
msg=socket.recv(缓冲区大小)
如果消息:
运行长度+=len(msg)
累加器+=味精
logger.debug(“已完成有效负载中的读取”)

^上述内容并不特别相关,但我觉得这可能有助于说明我为什么要做我正在做的事情。

从@Epsi95中得到提示,那么:

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

arr.split(b'\'')
表示用字符串中不包含的单引号分割。是否需要
list(arr)
?啊,废话。我不是故意的。我有点盲目地抄袭了帖子的建议。在任何情况下,有人对如何将bytearray拆分为单个字节列表有什么建议吗?