一、str.encode(‘编码’) 将特定字符编码
一、bytes.decode(‘编码’) 将字符编码解码为字符文本
应用一:
s1 =‘ABCD’
print(s1.encode(‘ASCII’))
s2 = ‘你好’
print(s2.encode(‘UTF-8’))
print(s2.encode(‘UTF-16’))
b1 = b’\xe4\xbd\xa0\xe5\xa5\xbd’
print(b1.decode(‘utf-8’))
print(s2.encode()) #不写编码的话系统默认时utf-8
#检测系统的默认编码是什么
import sys
print(sys.getdefaultencoding())
应用二:
open(‘data.txt’,‘w’,encoding=‘utf8’).write(‘你好’)
print(open(‘data.txt’,‘r’,encoding=‘utf8’).read())
字节与文本的编码、解码
最新推荐文章于 2023-06-30 10:59:14 发布