How do I initialize ('declare') an empty bytes
variable in Python 3?
I am trying to receive chunks of bytes, and later change that to a utf-8 string. However, I'm not sure how to initialize the initial variable that will hold the entire series of bytes. This variable is called msg
. I can't initialize it as None
, because you can't add a bytes
and a NoneType
. I can't initialize it as a unicode string, because then I will be trying to add bytes
to a string. Also, as the receiving program evolves it might get me in to a mess with series of bytes that contain only parts of characters. I can't do without a msg
initialization, because then msg
would be referenced before assignment. The following is the code in question
def handleClient(conn, addr): print('Connection from:', addr) msg = ? while 1: chunk = conn.recv(1024) if not chunk: break msg = msg + chunk msg = str(msg, 'UTF-8') conn.close() print('Received:', unpack(msg))
Since all three arguments are optional, we can pass an empty string to generate an empty byte array (Byte array of size 0). Depending on the type of the source parameter, an appropriate byte array will be initialized. If source is a String, Python bytes() will convert the string to bytes using str. encode() .
bytes() takes three optional parameters: source (Optional) - source to initialize the array of bytes. encoding (Optional) - if the source is a string, the encoding of the string. errors (Optional) - if the source is a string, the action to take when the encoding conversion fails (Read more: String encoding)
Python supports a range of types to store sequences. There are six sequence types: strings, byte sequences (bytes objects), byte arrays (bytearray objects), lists, tuples, and range objects. Strings contain Unicode characters.
Just use an empty byte string, b''
.
However, concatenating to a string repeatedly involves copying the string many times. A bytearray
, which is mutable, will likely be faster:
msg = bytearray() # New empty byte array # Append data to the array msg.extend(b"blah") msg.extend(b"foo")
To decode the byte array to a string, use msg.decode(encoding='utf-8')
.
Use msg = bytes('', encoding = 'your encoding here')
.
Encase you want to go with the default encoding, simply use msg = b''
, but this will garbage the whole buffer if its not in the same encoding
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With