13

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.

2
  • Why do you need it to work in both? You don't have separate versions for each? Commented Apr 20, 2015 at 15:14
  • 1
    It's library code and if I can have it be version agnostic, then all the better Commented Apr 20, 2015 at 15:15

3 Answers 3

11

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

data = bytes(str(data).encode("utf-8"))
Sign up to request clarification or add additional context in comments.

Comments

10

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.

7 Comments

This will also fail for older versions of python
@PadraicCunningham what is the minimum Python version this works for ?
The python community has adopted the EAFP (easier to ask forgiveness than permission) philosophy, so actually the try/except is more pythonic than this.
@cowlinator You are correct if you can catch a specific exception. Catching a general exception is bad practice.
Keep in mind this will fail in Python 2 if data has non-ascii characters.
|
6

If you're using the six py2/3 compatibility library, you may prefer:

import six
data = bytes(data) if six.PY2 else bytes(data, 'utf8')

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.