Just use a bytearray() which is a list of bytes.
Python2:
s = "ABCD"
b = bytearray()
b.extend(s)
Python3:
s = "ABCD"
b = bytearray()
b.extend(map(ord, s))
By the way, don't use str as a variable name since that is builtin.
Just use a bytearray() which is a list of bytes.
Python2:
s = "ABCD"
b = bytearray()
b.extend(s)
Python3:
s = "ABCD"
b = bytearray()
b.extend(map(ord, s))
By the way, don't use str as a variable name since that is builtin.
encode function can help you here, encode returns an encoded version of the string
In [44]: str = "ABCD"
In [45]: [elem.encode("hex") for elem in str]
Out[45]: ['41', '42', '43', '44']
or you can use array module
In [49]: import array
In [50]: print array.array('B', "ABCD")
array('B', [65, 66, 67, 68])
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
How to convert a list of bytes (Unicode) to a Python string? - Stack Overflow
Convert a list of bytes to a string in Python 3 - Stack Overflow
How Do I Convert a List to a Byte-Like Object?
Convert bytes to a string in Python 3 - Stack Overflow
Converting a sequence of bytes to a Unicode string is done by calling the decode() method on that str (in Python 2.x) or bytes (Python 3.x) object.
If you actually have a list of bytes, then, to get this object, you can use ''.join(bytelist) or b''.join(bytelist).
You need to specify the encoding that was used to encode the original Unicode string.
However, the term "Python string" is a bit ambiguous and also version-dependent. The Python str type stands for a byte string in Python 2.x and a Unicode string in Python 3.x. So, in Python 2, just doing ''.join(bytelist) will give you a str object.
Demo for Python 2:
In [1]: 'ัะตัั'
Out[1]: '\xd1\x82\xd0\xb5\xd1\x81\xd1\x82'
In [2]: bytelist = ['\xd1', '\x82', '\xd0', '\xb5', '\xd1', '\x81', '\xd1', '\x82']
In [3]: ''.join(bytelist).decode('utf-8')
Out[3]: u'\u0442\u0435\u0441\u0442'
In [4]: print ''.join(bytelist).decode('utf-8') # encodes to the terminal encoding
ัะตัั
In [5]: ''.join(bytelist) == 'ัะตัั'
Out[5]: True
you can also convert the byte list into string list using the decode()
stringlist=[x.decode('utf-8') for x in bytelist]
IMO, this is slightly more readable, but I wouldn't complain if I came across your code in review. Tested in Python 2.7:
>>> bytearray([b'F', b'o', b'o']).decode('ascii')
u'Foo'
If you don't like how join looks, you can do following:
bytes.join(b'', [b'F', b'o', b'o']).decode('ascii')
It's almost the same thing as in your code. I don't think you'll find any better approach.
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.
Decode the bytes object to produce a string:
>>> b"abcde".decode("utf-8")
'abcde'
The above example assumes that the bytes object is in UTF-8, because it is a common encoding. However, you should use the encoding your data is actually in!
Decode the byte string and turn it in to a character (Unicode) string.
Python 3:
encoding = 'utf-8'
b'hello'.decode(encoding)
or
str(b'hello', encoding)
Python 2:
encoding = 'utf-8'
'hello'.decode(encoding)
or
unicode('hello', encoding)
i am stuck in python programming. i read a line from the serial port
line=ser.readline() print(line)
when the above code is executed i get like
b'-83,-156,-205\r\n'
but i want only values like a=-83,b=-156,c=-205. how to extract these values/ how to get these values?