简体   繁体   中英

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)

TypeError: sequence item 0: expected a bytes-like object, int found

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.extend(byt2)

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

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

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