Bytes-like objects are objects that behave like the built-in bytes
type in Python. They represent a sequence of integers in the range 0-255 and can be used to store binary data. In Python, several classes and functions allow you to create and manipulate bytes-like objects.
Here are a few examples to get you started:
Using the bytes
class
The bytes
class is the simplest way to create a bytes-like object in Python. You can create a bytes
object by passing a string of characters to the bytes
constructor:
>>> b = bytes("Hello, world!", "utf-8")
>>> print(b)
b'Hello, world!'
Note that the string of characters must be encoded in a specific encoding (in this case, UTF-8), as it needs to be translated into a sequence of integers in the range 0-255.
You can access individual bytes of a bytes
object using square brackets, just like a list:
>>> print(b[0])
72
>>> print(b[1])
101
Using the bytearray
class
The bytearray
class is similar to the bytes
class, but it allows you to modify the contents of the object. You can create a bytearray
object using the bytearray
constructor:
>>> ba = bytearray("Hello, world!", "utf-8")
>>> print(ba)
bytearray(b'Hello, world!')
Just like with bytes
, the string of characters must be encoded in a specific encoding.
You can modify individual bytes of a bytearray
object using square brackets:
>>> ba[0] = 74
>>> print(ba)
bytearray(b'Jello, world!')
Using the bytes
function
The bytes
function is another way to create a bytes-like object in Python. Unlike the bytes
class, the bytes
function allows you to create a bytes
object from a sequence of integers:
>>> b = bytes([72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33])
>>> print(b)
b'Hello, world!'
Note that the integers must be in the range 0-255, as they represent individual bytes of binary data.
Using memoryview
The memoryview
class is a way to access the memory of a bytes-like object without copying it. You can create a memoryview
object using the memoryview
constructor:
>>> b = b'Hello, world!'
>>> mv = memoryview(b)
>>> print(mv)
<memory at 0x7f9a7c0d9a88>
You can access the contents of a memoryview
object using square brackets, just like a bytes
object:
>>> print(mv[0])
72
>>> print(mv[1])
101
Note that a memoryview
object cannot be modified directly. If you need to modify the contents of a bytes-like object, use a bytearray
object instead.
These are just a few examples of
Encoding and decoding binary data
When working with binary data, you may need to encode or decode it to a different representation, such as a string or a list of integers. Here are a few examples:
Encoding with bytes.decode()
The bytes.decode()
method allows you to encode a bytes
object into a string using a specific encoding, such as UTF-8:
>>> b = b'Hello, world!'
>>> s = b.decode("utf-8")
>>> print(s)
Hello, world!
Decoding with str.encode()
The str.encode()
method allows you to decode a string into a bytes
object using a specific encoding, such as UTF-8:
>>> s = "Hello, world!"
>>> b = s.encode("utf-8")
>>> print(b)
b'Hello, world!'
Converting to a list of integers with list()
You can convert a bytes
object into a list of integers using the list()
function:
>>> b = b'Hello, world!'
>>> l = list(b)
>>> print(l)
[72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33]
Converting from a list of integers with bytes()
You can convert a list of integers into a bytes
object using the bytes()
function:
>>> l = [72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33]
>>> b = bytes(l)
>>> print(b)
b'Hello, world!'
Binary data file I/O
When working with binary data, you may need to read from or write to a file on disk. Here are a few examples:
Writing binary data to a file with open()
and write()
You can write binary data to a file using the open()
function and the write()
method:
>>> b = b'Hello, world!'
>>> with open("example.bin", "wb") as f:
... f.write(b)
...
>>>
Note that you must open the file in binary write mode ("wb"
) instead of the default text write mode ("w"
).
Reading binary data from a file with open()
and read()
You can read binary data from a file using the open()
function and the read()
method:
>>> with open("example.bin", "rb") as f:
... b = f.read()
... print(b)
...
b'Hello, world!'
>>>
Note that you must open the file in binary read mode ("rb"
) instead of the default text read mode ("r"
).
Conclusion
Bytes-like objects are a versatile and important data type in Python, allowing you to store and manipulate binary data. Whether you're reading or writing binary data to a file, encoding or decoding binary data, or creating and modifying bytes-like objects, there are many tools and techniques available to you
Popular questions
Here are five questions about bytes-like objects with answers and code examples:
- What is a bytes-like object in Python?
A bytes-like object is an object that behaves like a bytes
object, with a similar interface and methods. A bytes
object is an immutable sequence of bytes, used to represent binary data.
>>> b = b'Hello, world!'
>>> type(b)
<class 'bytes'>
>>>
- How do you create a bytes-like object in Python?
You can create a bytes
object using the bytes()
constructor, passing in an iterable of integers representing the byte values:
>>> b = b'Hello, world!'
>>> b = bytes([72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33])
>>> print(b)
b'Hello, world!'
- How do you encode a bytes-like object as a string in Python?
You can use the bytes.decode()
method to encode a bytes
object into a string using a specific encoding, such as UTF-8:
>>> b = b'Hello, world!'
>>> s = b.decode("utf-8")
>>> print(s)
Hello, world!
- How do you decode a string as a bytes-like object in Python?
You can use the str.encode()
method to decode a string into a bytes
object using a specific encoding, such as UTF-8:
>>> s = "Hello, world!"
>>> b = s.encode("utf-8")
>>> print(b)
b'Hello, world!'
- How do you write binary data to a file in Python?
You can write binary data to a file using the open()
function and the write()
method:
>>> b = b'Hello, world!'
>>> with open("example.bin", "wb") as f:
... f.write(b)
...
>>>
Note that you must open the file in binary write mode ("wb"
) instead of the default text write mode ("w"
).
Tag
Binary