Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tuple to bytes
#1
How can I convert a tuple to bytes ?
Reply
#2
bytearray?
Reply
#3
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.
Reply
#4
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'>
Reply
#5
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  code with no tuple gets : IndexError: tuple index out of range Aggam 4 2,802 Nov-04-2020, 11:26 AM
Last Post: Aggam
  How to get first line of a tuple and the third item in its tuple. Need Help, Anybody? SukhmeetSingh 5 3,187 May-21-2019, 11:39 AM
Last Post: avorane
  replace bytes with other byte or bytes BigOldArt 1 10,608 Feb-02-2019, 11:00 PM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020