Working with UTF-8 encoding in Python source
Here is a code snippet that demonstrates how to work with UTF-8 encoding in a Python source file:
Here is a code snippet that demonstrates how to work with UTF-8 encoding in a Python source file:
UTF-8 encoding in a Python source file
python— editable, runs on the server
In this example, the encode() method is used to convert a string to a UTF-8 encoded byte sequence, and the decode() method is used to convert a UTF-8 encoded byte sequence back to a string.
You can also read and write files with utf-8 encoding
Read and write files with utf-8 encoding
with open("file.txt", "w", encoding="utf-8") as f:
f.write("Hello, 世界")
with open("file.txt", "r", encoding="utf-8") as f:
print(f.read()) # 'Hello, 世界'