String to bytes in both Python 2 and 3

Xophmeister

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.

Alex

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Compare string to bytes that works in both Python 2 and 3

From Dev

Writing bytes to standard output in a way compatible with both, python2 and python3

From Dev

Unicode string to bytes python 3

From Dev

using `bytes` in python 2 and 3

From Java

Best way to convert string to bytes in Python 3?

From Dev

Python3 bytes to hex string

From Dev

Problems with decoding bytes into string or ASCII in python 3

From Dev

String / Bytes issue when upgrading from Python2 to Python3

From Dev

How to convert string to bytes in Python 2

From Dev

How to convert string to bytes in Python 2

From Dev

Import structure that works both in packages and out, in both Python 2 and 3?

From Dev

Convert from mac address to hex string and vice versa - both python 2 and 3

From Java

Python 2 vs 3 raw bytes output

From Dev

Chunking bytes (not strings) in Python 2 and 3

From Dev

Supporting python 2 and 3: str, bytes or alternative

From Dev

How to overwrite in both python 2 and 3?

From Dev

Python 3 - if a string contains only ASCII, is it equal to the string as bytes?

From Dev

Python 3 - if a string contains only ASCII, is it equal to the string as bytes?

From Dev

Convert from string containing hexadecimal characters to bytes in python 3

From Dev

What is the correct way to convert bytes to a hex string in Python 3?

From Dev

python 2.7 vs 3 encode decode bytes string

From Dev

Python converting bytes to string

From Dev

Python String to Hex Bytes

From Dev

Create a virtualenv with both python2 and python3

From Dev

How to judge a file type both work in python 2 and python 3

From Dev

Python 2 and 3 compatible method to convert bytes to integer

From Dev

Python 2 and 3 compatible method to convert bytes to integer

From Dev

binary using both Python C API version 2 and 3

From Java

Python : Get size of string in bytes

Related Related

  1. 1

    Compare string to bytes that works in both Python 2 and 3

  2. 2

    Writing bytes to standard output in a way compatible with both, python2 and python3

  3. 3

    Unicode string to bytes python 3

  4. 4

    using `bytes` in python 2 and 3

  5. 5

    Best way to convert string to bytes in Python 3?

  6. 6

    Python3 bytes to hex string

  7. 7

    Problems with decoding bytes into string or ASCII in python 3

  8. 8

    String / Bytes issue when upgrading from Python2 to Python3

  9. 9

    How to convert string to bytes in Python 2

  10. 10

    How to convert string to bytes in Python 2

  11. 11

    Import structure that works both in packages and out, in both Python 2 and 3?

  12. 12

    Convert from mac address to hex string and vice versa - both python 2 and 3

  13. 13

    Python 2 vs 3 raw bytes output

  14. 14

    Chunking bytes (not strings) in Python 2 and 3

  15. 15

    Supporting python 2 and 3: str, bytes or alternative

  16. 16

    How to overwrite in both python 2 and 3?

  17. 17

    Python 3 - if a string contains only ASCII, is it equal to the string as bytes?

  18. 18

    Python 3 - if a string contains only ASCII, is it equal to the string as bytes?

  19. 19

    Convert from string containing hexadecimal characters to bytes in python 3

  20. 20

    What is the correct way to convert bytes to a hex string in Python 3?

  21. 21

    python 2.7 vs 3 encode decode bytes string

  22. 22

    Python converting bytes to string

  23. 23

    Python String to Hex Bytes

  24. 24

    Create a virtualenv with both python2 and python3

  25. 25

    How to judge a file type both work in python 2 and python 3

  26. 26

    Python 2 and 3 compatible method to convert bytes to integer

  27. 27

    Python 2 and 3 compatible method to convert bytes to integer

  28. 28

    binary using both Python C API version 2 and 3

  29. 29

    Python : Get size of string in bytes

HotTag

Archive