Created
September 26, 2023 06:29
-
-
Save nenetto/7beaaa00f8218e495414a04d74f444a5 to your computer and use it in GitHub Desktop.
Python bytes, string and unicode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def to_str(bytes_or_str): | |
"""Transform to string UTF-8""" | |
if isinstance(bytes_or_str, bytes): | |
value = bytes_or_str.decode(‘utf-8’) | |
else: | |
value = bytes_or_str | |
return value # Instance of str | |
def to_bytes(bytes_or_str): | |
"""Transform to bytestring""" | |
if isinstance(bytes_or_str, str): | |
value = bytes_or_str.encode(‘utf-8’) | |
else: | |
value = bytes_or_str | |
return value # Instance of bytes |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment