str = "我是中国人"
print(f'Unicode字符串为"{str}"')
byte0 = str.encode("utf-8")
print(f'Unicode字符串"{str}"以utf-8编码得到的字符串为[{byte0}]')
str0 = byte0.decode('utf-8')
print(f'utf-8字符串"{byte0}"以utf-8解码得到的字符串为"{str0}"')
byte1 = str.encode("gbk")
print(f'Unicode字符串"{str}"以gbk编码得到的字符串为[{byte1}]')
str1 = byte1.decode('gbk')
print(f'utf-8字符串"{byte1}"以gbk解码得到的字符串为"{str1}"')
print("")
print(f'以文本的形式将Unicode编码的字符串"{str}"写入a.txt')
with open("a.txt", "w", encoding="gbk") as f:
f.write(str)
print("")
print(f'以文本的方式读取a.txt')
with open("a.txt", "r", encoding="gbk") as f:
print(f.read())
```python

python中编码转换
于 2025-07-15 11:13:14 首次发布
1894

被折叠的 条评论
为什么被折叠?



