Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 3 How do I 'declare' an empty `bytes` variable

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)) 
like image 398
tsteemers Avatar asked May 21 '13 19:05

tsteemers


People also ask

How do you create an empty byte in Python?

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

How do you initialize bytes in Python?

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)

Is there a byte type in Python?

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.


2 Answers

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').

like image 91
Mechanical snail Avatar answered Sep 27 '22 23:09

Mechanical snail


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

like image 38
Dr. Sahib Avatar answered Sep 27 '22 23:09

Dr. Sahib