You can pass the bytes string into list() to convert back:
>>> lst = [1, 2, 3, 4, 5]
>>> bytes(lst)
b'\x01\x02\x03\x04\x05'
>>> list(bytes(lst))
[1, 2, 3, 4, 5]
Answer from ggorlen on Stack Overflow
I am trying to pass a list to an OS command using subprocess.communicate, but can't seem to get it to use my list. I did some researching and thought I was supposed to use bytes to convert my list to a byte-like object, but bytes does not take lists. I am very new to Python and having a hard time figuring this out. My functions is:
def prompt_for_input(list_of_programs, prompt_menu, extra_arguments, prompt_string):
## Variable declarations.
user_selection = None
## If extra_arguments were passed, then reform prompt_menu
## to contain the extra_arguments.
if extra_arguments:
## Get the prompt argument from prompt_menu.
prompt_argument = "-" + prompt_menu.split('-')[1]
## Reform prompt_menu to include extra_arguments.
prompt_argument_index = prompt_menu.index(prompt_argument)
prompt_menu = prompt_menu[:prompt_argument_index] + extra_arguments +\
" " + prompt_menu[prompt_argument_index:]
## Add prompt_string to prompt_menu.
prompt_menu = prompt_menu + " " + prompt_string
## Get the user_selection.
process = subprocess.Popen(shlex.split(prompt_menu), stdout=subprocess.PIPE,\
stdin=subprocess.PIPE)
user_selection = process.communicate(input=bytes(list_of_programs))I can't seem to find any solid information on how to convert this, could someone point me in the right direction?
Edit
-
Updated the code above to match with master.
How to get back a list from bytes in Python? - Stack Overflow
Lists to Bytes
python - Convert bytes into list - Stack Overflow
arrays - allocating a bytes list in Python - Stack Overflow
I am trying to convert a list into data-type bytes so I can encrypt it. Anyone knows how to do it for a list with strings. Thanks
Here's a example:
list_of_items = ["Cats", "Dogs","Turtle","Humans"]
list_of_items_byte = bytes(list_of_items,"utc-8")
and I get this error:
File "main.py", line 2, in <module>
list_of_items_byte = bytes(list_of_items,"utc-8")
TypeError: encoding without a string argument
We can use the following line:
[x.decode("utf8") for x in data.split(b"\x00") if len(x)]
Going part by part:
x.decode("utf8"):xwill be a bytes string, so we need to convert it into a string via `.decode("utf8").for x in data.split(b"\x00"): We can use python's built inbytes.splitmethod in order to split the byte string by the nullbytes to get an array of individual strings.if len(x): This is equivalent toif len(x) > 0, since we want to discard the empty string at the end.
This code may help you to understand if you want exact same output using the pop() function.
data = 'red/x00XY/x001/x00168.93/x00859.07/x00' # I change "/" mark from "\" because i'm using Linux otherwise it will give error in Linux
new_list = [] # There is a variable that contain empty list
for item in data.split('/x00'): # Here I use split function by default it splits variable where "," appears but in this case
new_list.append(item) # you need list should be separated by "/" so that's why I gave split('/x00') and one by list appended
print(new_list)