编码拾遗
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() 函數(shù)將對象轉(zhuǎn)化為供解釋器讀取的形式。 '中國'
?
?
?
?
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 #設(shè)置系統(tǒng)默認(rèn)編碼,執(zhí)行dir(sys)時(shí)不會(huì)看到這個(gè)方法, 13 # 在解釋器中執(zhí)行不通過,可以先執(zhí)行reload(sys), 14 # 在執(zhí)行 setdefaultencoding('utf8'), 15 # 此時(shí)將系統(tǒng)默認(rèn)編碼設(shè)置為utf8。(見設(shè)置系統(tǒng)默認(rèn)編碼 ) 16 reload(sys)# 17 sys.setdefaultencoding('utf8')#直接設(shè)置的時(shí)候,sys 不會(huì)顯示出setdefaultencoding屬性,不能調(diào)用。先reload(sys)#再設(shè)置 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 """ View Code?
轉(zhuǎn)載于:https://www.cnblogs.com/Mengchangxin/p/9486595.html
總結(jié)
- 上一篇: 暑假第十七测
- 下一篇: 做梦梦到谁说明对方在想你