175. Unicode and Encoding
Snippet 1: Encoding and Decoding Strings
Copy
# 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
Copy
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
Copy
Snippet 4: Handling Encoding Errors with errors Parameter
Copy
Snippet 5: Detecting File Encoding with chardet
Copy
Snippet 6: Converting Between Encodings
Copy
Snippet 7: Unicode Normalization
Copy
Snippet 8: Handling Unicode with Regular Expressions
Copy
Snippet 9: Writing a File with a Non-UTF-8 Encoding
Copy
Snippet 10: Reading a File with Different Encodings
Copy
Last updated