Python Forum

Full Version: Tuple to bytes
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
How can I convert a tuple to bytes ?
Here is the example of the tuple below :

# define tuple for http header
t_header = (
    "Content-type: image/png \r",
    "Content-Language: pt-BR \r",
)

b_header = bytearray(t_header, 'utf-8')
print(type(b_header))
Error msg : TypeError: encoding without a string argument ?
Obs : I need the data type to be in bytes.
t_header = (
    "Content-type: image/png \r",
    "Content-Language: pt-BR \r",
)
 
b_header = bytearray(str(t_header), 'utf-8')
print(type(b_header))
Output:
<class 'bytearray'>
Seems like a really odd thing to do. I would understand converting a string to byte array. What makes you think you need a tuple? As far as I know, tuples don't exist outside of Python.