175. Unicode and Encoding

Snippet 1: Encoding and Decoding Strings

# Encoding a string to bytes
text = "Hello, World!"
encoded_text = text.encode("utf-8")
print(f"Encoded text: {encoded_text}")

# Decoding bytes back to string
decoded_text = encoded_text.decode("utf-8")
print(f"Decoded text: {decoded_text}")

Snippet 2: Writing Unicode Data to a File

text = "Hello, 世界"  # "Hello, World" in Chinese
with open("unicode_file.txt", "w", encoding="utf-8") as file:
    file.write(text)
print("Text written to file with UTF-8 encoding.")

Snippet 3: Reading Unicode Data from a File

with open("unicode_file.txt", "r", encoding="utf-8") as file:
    text = file.read()
    print(f"Read text from file: {text}")

Snippet 4: Handling Encoding Errors with errors Parameter


Snippet 5: Detecting File Encoding with chardet


Snippet 6: Converting Between Encodings


Snippet 7: Unicode Normalization


Snippet 8: Handling Unicode with Regular Expressions


Snippet 9: Writing a File with a Non-UTF-8 Encoding


Snippet 10: Reading a File with Different Encodings


Last updated