*Memo:
- My post explains a bytearray.
- My post explains the bytearray with indexing, slicing and copy.
- My post explains a bytes.
- My post explains a string.
bytearray() can create a bytearray with or without several types of objects or can encode a string to a bytearray as shown below:
*Memo to create a bytearray:
- The 1st argument is
source
(Optional-Default:b''
-Type:Bytes-like object/Iterable(int
)/int
):-
int
gives a null value(\x00
) which represents no value.
-
*Memo to encode a string to a bytearray:
- The 1st argument is
source
(Required-Type:str
). - The 2nd argument is
encoding
(Required-Type:str
):-
'utf-8'
,'utf-7'
,'utf-16'
,'big5'
,'ascii'
, etc can be set to it. - You can see Standard Encodings for more possible values.
-
- The 3rd argument is
errors
(Optional-Default:'strict'
-Type:str
):- It controls encoding error with the error handlers,
'strict'
,'ignore'
,'replace'
,'xmlcharrefreplace'
,'backslashreplace'
, etc. -
'strict'
raises UnicodeError if the character or byte, which cannot be encoded or decoded, exists. -
'ignore'
ignores the character or byte which cannot be encoded or decoded. -
'replace'
replaces the character or byte, which cannot be encoded or decoded, with?
for encoding or�
for decoding. -
'xmlcharrefreplace'
replaces the character, which cannot be encoded, with the XML/HTML numeric character reference format&#num;
:- It doesn't support decoding so error occurs for the byte which cannot be decoded while error doesn't occur for the byte which can be decoded.
-
'backslashreplace'
replaces the character or byte, which cannot be encoded or decoded, with the hexadecimal format\xhh
,\uxxxx
or\Uxxxxxxxx
for encoding or\xhh
for decoding. - You can see more error handlers.
- You can create your own error handler with codecs.register_error().
- It controls encoding error with the error handlers,
<Create a bytearray>:
v = bytearray() # Empty bytearray v = bytearray(b'') # Empty bytearray v = bytearray(source=b'12') # bytes v = bytearray(source=bytearray(b'12')) # bytearray v = bytearray(source=12) # int v = bytearray(source=True) # bool v = bytearray(source=[1, 2, 3]) # list(int) v = bytearray(source=(1, 2, 3)) # tuple(int) v = bytearray(source={1, 2, 3}) # set(int) v = bytearray(source=frozenset({1, 2, 3})) # frozenset(int) v = bytearray(source={1:2, 3:4}) # dict(int:int) v = bytearray(source={1:2, 3:4}.keys()) # dict(int:int).keys() v = bytearray(source={1:2, 3:4}.values()) # dict(any:int).values() v = bytearray(source=iter([1, 2, 3])) # iterator(int) v = bytearray(source=range(10, 20)) # range # No error print(type(bytearray())) # <class 'bytes'> v = bytearray(source='12') # str v = bytearray(source=1.2) # float v = bytearray(source=1.2+3.4j) # complex v = bytearray(source={1:2, 3:4}.items()) # dict(int:int).items() v = bytearray(source=lambda: 10) # function # Error
# Empty bytearray v = bytearray() v = bytearray(b'') print(v) # bytearray(b'')
v = bytearray(source=b'12') # bytes v = bytearray(source=bytearray(b'12')) # bytearray print(v, v[0], v[1]) # bytearray(b'12') 49 50
v = bytearray(source=12) # int print(v, v[0], v[1], v[2], v[3], v[4]) # bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00') 0 0 0 0 0
v = bytearray(source=True) # bool print(v, v[0]) # bytearray(b'\x00') 0
v = bytearray(source=[1, 2, 3]) # list print(v, v[0], v[1], v[2]) # bytearray(b'\x01\x02\x03') 1 2 3
v = bytearray(source=(1, 2, 3)) # tuple print(v, v[0], v[1], v[2]) # bytearray(b'\x01\x02\x03') 1 2 3
v = bytearray(source={1, 2, 3}) # set(int) v = bytearray(source=frozenset({1, 2, 3})) # frozenset(int) print(v, v[0], v[1], v[2]) # bytearray(b'\x01\x02\x03') 1 2 3
v = bytearray(source={1:2, 3:4}) # dict(int:int) v = bytearray(source={1:2, 3:4}.keys()) # dict(int:int).keys() print(v, v[0], v[1]) # bytearray(b'\x01\x03') 1 3
v = bytearray(source={1:2, 3:4}.values()) # dict(int:int).values() print(v, v[0], v[1]) # bytearray(b'\x02\x04') 2 4
v = bytearray(source=iter([1, 2, 3])) # iterator(int) print(v, v[0], v[1], v[2]) # bytearray(b'\x01\x02\x03') 1 2 3
v = bytearray(source=range(10, 20)) # range print(v, v[0], v[1], v[2], v[3], v[4], v[5], v[6]) # bytearray(b'\n\x0b\x0c\r\x0e\x0f\x10\x11\x12\x13') 10 11 12 13 14 15 16
<Encode a string to a bytearray>:
v = "Lёт's gφ!" # Let's go! print(bytearray(source=v, encoding='utf-8')) print(bytearray(source=v, encoding='utf-8', errors='strict')) # bytearray(b"L\xd1\x91\xd1\x82\'s g\xcf\x86!")
v = "Lёт's gφ!" # Let's go! print(bytearray(source=v, encoding='utf-7')) # bytearray(b"L+BFEEQg\'s g+A8Y!")
v = "Lёт's gφ!" # Let's go! print(bytearray(source=v, encoding='utf-16')) # bytearray(b"\xff\xfeL\x00Q\x04B\x04\'\x00s\x00 \x00g\x00\xc6\x03!\x00")
v = "Lёт's gφ!" # Let's go! print(bytearray(source=v, encoding='big5')) # bytearray(b"L\xc7\xce\xc7\xdb\'s g\xa3p!")
import codecs def hashreplace_handler(x): return ((x.end - x.start) * '#', x.end) codecs.register_error('hashreplace', hashreplace_handler) v = "Lёт's gφ!" # Let's go! print(bytearray(source=v, encoding='ascii', errors='ignore')) # bytearray(b"L\'s g!") print(bytearray(source=v, encoding='ascii', errors='replace')) # bytearray(b"L??\'s g?!") print(bytearray(source=v, encoding='ascii', errors='xmlcharrefreplace')) # bytearray(b"Lёт\'s gφ!") print(bytearray(source=v, encoding='ascii', errors='backslashreplace')) # bytearray(b"L\\u0451\\u0442\'s g\\u03c6!") print(bytearray(source=v, encoding='ascii', errors='hashreplace')) # bytearray(b"L##\'s g#!") print(bytearray(source=v, encoding='ascii', errors='strict')) # UnicodeEncodeError: 'ascii' codec can't encode characters # in position 1-2: ordinal not in range(128)
Top comments (0)