编码拾遗

 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'xe4xb8xadxe5x9bxbd' <class 'bytes'>
21 b2=b'xe4xb8xadxe5x9bxbd'
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() 函数将对象转化为供解释器读取的形式。 '中国'


 1 #!/usr/bin/env python
 2 #-*- coding:utf-8 -*-
 3 '''
 4 Administrator 
 5 2018/8/16 
 6 '''
 7 import sys
 8 print(sys.getdefaultencoding())
 9 print(dir(sys))
10 
11 
12 #设置系统默认编码,执行dir(sys)时不会看到这个方法,
13 # 在解释器中执行不通过,可以先执行reload(sys),
14 # 在执行 setdefaultencoding('utf8'),
15 # 此时将系统默认编码设置为utf8。(见设置系统默认编码 )
16 reload(sys)#
17 sys.setdefaultencoding('utf8')#直接设置的时候,sys 不会显示出setdefaultencoding属性,不能调用。先reload(sys)#再设置
18 print(sys.getdefaultencoding())
19 print(dir(sys))
20 print "牛油刀"
21 """
22 "D:Program Files (x86)Python27python.exe" "F:/python从入门到放弃/8.16/py27 编码.py"
23 ascii
24 ['__displayhook__', '__doc__', '__excepthook__', '__name__', '__package__', '__stderr__', '__stdin__', '__stdout__', '_clear_type_cache', '_current_frames', '_getframe', '_git', 'api_version', 'argv', 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright', 'displayhook', 'dllhandle', 'dont_write_bytecode', 'exc_clear', 'exc_info', 'exc_type', 'excepthook', 'exec_prefix', 'executable', 'exit', 'flags', 'float_info', 'float_repr_style', 'getcheckinterval', 'getdefaultencoding', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof', 'gettrace', 'getwindowsversion', 'hexversion', 'long_info', 'maxint', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'py3kwarning', 'setcheckinterval', 'setprofile', 'setrecursionlimit', 'settrace', 'stderr', 'stdin', 'stdout', 'subversion', 'version', 'version_info', 'warnoptions', 'winver']
25 utf8
26 ['__displayhook__', '__doc__', '__excepthook__', '__name__', '__package__', '__stderr__', '__stdin__', '__stdout__', '_clear_type_cache', '_current_frames', '_getframe', '_git', 'api_version', 'argv', 'builtin_module_names', 'byteorder', 'call_tracing', 'callstats', 'copyright', 'displayhook', 'dllhandle', 'dont_write_bytecode', 'exc_clear', 'exc_info', 'exc_type', 'excepthook', 'exec_prefix', 'executable', 'exit', 'flags', 'float_info', 'float_repr_style', 'getcheckinterval', 'getdefaultencoding', 'getfilesystemencoding', 'getprofile', 'getrecursionlimit', 'getrefcount', 'getsizeof', 'gettrace', 'getwindowsversion', 'hexversion', 'long_info', 'maxint', 'maxsize', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_cache', 'platform', 'prefix', 'py3kwarning', 'setcheckinterval', 'setdefaultencoding', 'setprofile', 'setrecursionlimit', 'settrace', 'stderr', 'stdin', 'stdout', 'subversion', 'version', 'version_info', 'warnoptions', 'winver']
27 牛油刀
28 
29 Process finished with exit code 0
30 """
View Code
原文地址:https://www.cnblogs.com/Mengchangxin/p/9486595.html