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() 函數將對象轉化為供解釋器讀取的形式。 '中國'
?
?
?
?


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)\Python27\python.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 """
?