Python关于编码和解码

s1 = '你好'
#如果是以‘w’的方式写入,写入前一定要进行encoding,否则会报错
with open('F:\\1.txt','w',encoding='utf-8') as f1:
f1.write(s1)
s2 = s1.encode("utf-8")#转换为bytes的形式
#这时候写入方式一定要是‘wb’,且一定不能加encoding参数
with open('F:\\2.txt','wb') as f2:
f2.write(s2)
https://www.cnblogs.com/chenhuabin/p/10111319.html
浙公网安备 33010602011771号