简体   繁体   English

Python:加入两个字节数组对象

[英]Python: join two bytearray objects

I would like to concatenate a bytearray to another bytearray.我想将一个字节数组连接到另一个字节数组。 I thought this might work:我认为这可能有效:

byt1 = bytearray(10)
byt2 = bytearray(10)
byt1.join(byt2)
print(repr(byt1))

byt1.join(byt2) byt1.join(byt2)

TypeError: sequence item 0: expected a bytes-like object, int found类型错误:序列项 0:预期是一个类似字节的对象,找到了 int

What is the most efficient way to achieve this?实现这一目标的最有效方法是什么?

Create a new combined bytearray from two:从两个创建一个新的组合字节数组:

byt_combined = byt1 + byt2

Extend one bytearray with another.用另一个扩展一个字节数组。 This changes byt1 :这改变了byt1

byt1.extend(byt2)

您可以将一个字节加入一个数组,如下所示:

    b"".join([bytearray(10), bytearray(10)])

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

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