lundi 20 avril 2015

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.

Aucun commentaire:

Enregistrer un commentaire