编码拾遗

 1 #!/usr/bin/env python3
 2 #-*- coding:utf-8 -*-
 3 '''
 4 Administrator 
 5 2018/8/16 
 6 '''
 7 
 8 # f=open("demo","r",encoding="utf8")
 9 # data=f.read()
10 # print(data)
11 # f.close()
12 
13 
14 # print("沈哲子")
15 
16 s="中国"   #str  unicode
17 b1=s.encode("utf8")   #编码 用 utf8    bytes
18 print(b1,type(b1))
19 
20 #b'\xe4\xb8\xad\xe5\x9b\xbd' <class 'bytes'>
21 b2=b'\xe4\xb8\xad\xe5\x9b\xbd'
22 # c1=b2.decode("utf8") #bytes    解码:str unicode
23 # print(c1,type(c1))
24 
25 b2=s.encode("gbk") #编码 用 gbk   bytes
26 print(b2,type(b2))
27 
28 c2=b2.decode("gbk")  #解码  用 gbk str
29 print(c2,type(c2))
30 
31 
32 print(repr(s))#repr() 函数将对象转化为供解释器读取的形式。 '中国'

猜你喜欢

转载自www.cnblogs.com/Mengchangxin/p/9486595.html