<aside> 👨🏼💻 Python supports a range of types to store sequences. Bytes and bytearray objects contain single bytes.
</aside>
Support my work buying me a coffee ☕
Martí Grau is R&D and IA engineer
This module provides functions for encoding binary data to printable ASCII characters and decoding such encodings back to binary data.
The interface supports:
encoding bytes-like objects to ASCII bytes
decoding bytes-like objects or strings containing ASCII to bytes.
base64.b64encode(s, altchars=None) → Encode the bytes-like objects using Base64 and return the encoded bytes.
base64.b64decode(s, altchars=None, validate=False) → Decode the Base64 encoded bytes-like object or ASCII string s and return the decoded bytes.
# Example
import base64
encoded = base64.b64encode(b'data to be encoded')
encoded
b'ZGF0YSB0byBiZSBlbmNvZGVk'
data = base64.b64decode(encoded)
data
b'data to be encoded'
The io module provides Python’s main facilities for dealing with various types of I/O.
Types:
text: Text I/O expects and produces str objects.
To create a text stream is with open(), (optionally specifying an encoding)
f = open("myfile.txt", "r", encoding="utf-8")
In-memory text streams are also available as StringIO objects:
f = io.StringIO("some initial text data")
Binary: Binary I/O expects bytes-like objects and produces bytes objects. (No encoding, decoding, or newline translation is performed.)
raw: Raw I/O generally used as a low-level building-block for binary and text streams
f = open("myfile.jpg", "rb", buffering=0)
Base casses: