设置字符串
base_str = "我已经将我的狗狗送人了"
print(type(base_str))
按照utf-8的格式转出bytes
bytes_utf_8 = base_str.encode(encoding="utf-8")
print(bytes_utf_8)
按照gb2312的格式转成bytes
bytes_gb2312 = base_str.encode(encoding="gb2312")
print(bytes_gb2312)
解码成string(使用utf-8的格式)
str_from_utf_8 = bytes_utf_8.decode(encoding="utf-8")
print(str_from_utf_8)s
解码成string(使用gb2312的格式)
str_from_gb2312 = bytes_gb2312.decode(encoding="gb2312")
print(str_from_gb2312)
依次输入,结果如下
C:\Python34\python3.exe C:/Users/cyjjkz1/PycharmProjects/request01/stu01.py
<class 'str'>
b'\xe6\x88\x91\xe5\xb7\xb2\xe7\xbb\x8f\xe5\xb0\x86\xe6\x88\x91\xe7\x9a\x84\xe7\x8b\x97\xe7\x8b\x97\xe9\x80\x81\xe4\xba\xba\xe4\xba\x86'
b'\xce\xd2\xd2\xd1\xbe\xad\xbd\xab\xce\xd2\xb5\xc4\xb9\xb7\xb9\xb7\xcb\xcd\xc8\xcb\xc1\xcb'
我已经将我的狗狗送人了
我已经将我的狗狗送人了
进程已结束,退出代码0