Posts: 4,647
Threads: 1,494
Joined: Sep 2016
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.
Posts: 6,798
Threads: 20
Joined: Feb 2020
Mar-09-2023, 07:14 PM
(This post was last modified: Mar-09-2023, 07:14 PM by deanhystad.)
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')
Posts: 4,647
Threads: 1,494
Joined: Sep 2016
Mar-09-2023, 11:09 PM
(This post was last modified: Mar-09-2023, 11:09 PM by Skaperen.)
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.
Posts: 4,647
Threads: 1,494
Joined: Sep 2016
Mar-09-2023, 11:44 PM
(This post was last modified: Mar-09-2023, 11:44 PM by Skaperen.)
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.
Posts: 6,798
Threads: 20
Joined: Feb 2020
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().
Posts: 4,647
Threads: 1,494
Joined: Sep 2016
(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.
Posts: 6,798
Threads: 20
Joined: Feb 2020
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.
Posts: 4,647
Threads: 1,494
Joined: Sep 2016
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.
Posts: 6,798
Threads: 20
Joined: Feb 2020
Mar-11-2023, 03:05 AM
(This post was last modified: Mar-11-2023, 03:05 AM by deanhystad.)
x is a byte. b'abc' is iterable.. You append(byte) and extend(iterable)
Posts: 4,647
Threads: 1,494
Joined: Sep 2016
Mar-12-2023, 05:52 PM
(This post was last modified: Mar-12-2023, 05:52 PM by Skaperen.)
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.
|