Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
appending to a bytearray
#1
i cannot find a way to append a bytes string to a bytearray other than += operator. is that the way it has to be done? bytearray.append() gives an error exception message saying it wants an int. apparently it only takes one at a time:
Output:
>>> ab=bytearray(b'foobar') >>> ab.append(120) >>> ab bytearray(b'foobarx') >>> ab.append(121,122) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: append() takes exactly one argument (2 given) >>> ab.append([121,122]) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'list' object cannot be interpreted as an integer >>> ab.append(121) >>> ab.append(122) >>> ab bytearray(b'foobarxyz') >>>
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
It is just like a list. append() for adding 1 value, extend() for adding many.
x = bytearray(b"bytes")
x.extend(b" and more bytes")
print(x)
Output:
bytearray(b'bytes and more bytes')
Skaperen likes this post
Reply
#3
thank you. i was thinking of a string, such as bytearray, as "one" value, so i never thought of .extend().
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
another way to extend a bytearray is with a slice:
Output:
lt1a/forums/3 /home/forums 13> py Python 3.8.10 (default, Nov 14 2022, 12:59:47) [GCC 9.4.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> x = bytearray(b"bytes") >>> x[len(x):]=b" and more bytes" >>> print(x) bytearray(b'bytes and more bytes') >>>
an even uglier way:
Output:
>>> print(x) bytearray(b'bytes and more bytes') >>> x[:]=x+b'and even more bytes' >>> print(x) bytearray(b'bytes and more bytesand even more bytes') >>>
but it's not that much uglier. and i bet these ways can be done with list (and tuples?).

edit:

yeah, i should'a put a space in there.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
A bytes object is one object, but so is a list. Both are iterable which is why they can be used as an argument to extend().
Reply
#6
(Mar-09-2023, 11:47 PM)deanhystad Wrote: A bytes object is one object, but so is a list. Both are iterable which is why they can be used as an argument to extend().
can i use any iterator that produces the expected types (just about anything for a list) as an argument in .extend()?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#7
Output:
>>> help(bytearray.extend) Help on method_descriptor: extend(self, iterable_of_ints, /) Append all the items from the iterator or sequence to the end of the bytearray. iterable_of_ints The iterable of items to append.
Reply
#8
what did i do wrong with this? how do i fix it?
Output:
>>> >>> b=bytearray() >>> for x in bytearray(b'abc'): ... b.extend(x) ... Traceback (most recent call last): File "<stdin>", line 2, in <module> TypeError: can't extend bytearray with int >>>
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#9
x is a byte. b'abc' is iterable.. You append(byte) and extend(iterable)
Reply
#10
so, how do i fix b.extend(x) to make it work, such that b ends up with whatever followed in in the loop control?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Value error when converting hex value to bytearray shubhamjainj 7 10,562 Mar-20-2023, 05:30 PM
Last Post: Skaperen
  Split Bytearray into separate Files by Hex delimter lastyle 5 2,676 Mar-09-2023, 07:49 AM
Last Post: bowlofred
  Save multiple Parts of Bytearray to File ? lastyle 1 961 Dec-10-2022, 08:09 AM
Last Post: Gribouillis
  bytearray object - why converting to ascii? Chepilo 2 1,643 Nov-21-2022, 07:25 PM
Last Post: Chepilo
  Bytearray substitution Vismuto 1 2,628 Apr-14-2020, 09:18 AM
Last Post: TomToad
  int() function with bytearray() Jona66 1 2,405 Sep-08-2019, 12:41 PM
Last Post: ichabod801
  Conversion needed from bytearray to Floating point braveYug 1 4,130 May-07-2018, 12:23 PM
Last Post: snippsat
  Windows DIB format in bytearray to image? dusca 2 2,772 Mar-28-2018, 10:35 PM
Last Post: dusca
  Bytearray questions mattps 2 14,412 Mar-25-2018, 06:54 AM
Last Post: mattps
  ByteArray outside while true Schampbakken 5 4,390 Feb-18-2018, 02:02 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020