Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String to bytes in both Python 2 and 3

My function needs to take input as either a string or binary data (e.g., read from a file). If it's a string, I want to convert this to raw data (bytes or bytearray).

In Python 3, I can do data = bytes(data, 'utf8'). However, this fails in Python 2 as it only takes one argument. Vice versa, data = bytes(data) works in Python 2, but not in Python 3 as it complains about needing an encoding to work to.

For the sake of argument, let's say that all input, if it comes as a string, is UTF-8 encoded. Is there then a better way to achieve what I'm looking for than the following monstrosity:

try:
  data = bytes(data, 'utf8')
except:
  data = bytes(data)

n.b., data.encode() works in Py3, but fails in Py2 in the case that the string contains non-ASCII bytes.

like image 802
Xophmeister Avatar asked Apr 20 '15 15:04

Xophmeister


2 Answers

This works with both version. i.e. python 2 and python 3

data = bytes(str(data).encode("utf-8"))
like image 174
Ankit Saran Avatar answered Oct 13 '22 16:10

Ankit Saran


You can check the version using sys.version_info:

if sys.version_info < (3, 0):
    data = bytes(data)
else:
    data = bytes(data, 'utf8')

It is more pythonic than relying on exceptions.

like image 9
Alex Avatar answered Oct 13 '22 15:10

Alex