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.
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.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments