格式化字符串/復(fù)合字段名
>>> import humansize>>> si_suffixes = humansize.SUFFIXES[1000]>>> si_suffixes['KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']>>> '1000{0[0]} = 1{0[1]}'.format(si_suffixes)'1000KB = 1MB'
>>> import humansize>>> import sys>>> '1MB = 1000{0.modules[humansize].SUFFIXES[1000][0]}'.format(sys)'1MB = 1000KB'
Sys.modules 是一個(gè)保存當(dāng)前python實(shí)例中搜有已導(dǎo)入模塊的字典。模塊的名字為鍵,模塊自身為值。
>>> s = '''finished files are the re-sults of years of scientific study combined with theexperience of years. ‘'' >>> s.splitlines()['finished files are the re-', 'sults of years of scientific study combined with the', 'experience of years. ‘] >>> PRint(s.lower())finished files are the re-sults of years of scientific study combined with theexperience of years.
>>> a_list = query.split("&")>>> a_list['user=pilgrim', 'database=master', ‘passWord=PapayaWh
split()-根據(jù)指定的分隔符,將字符串分隔成一個(gè)字符串列表。
dict() - 將包含列表的列表轉(zhuǎn)換成字典對(duì)象
字符串的分片
>>> a_string = "My alphabet starts where your alphabet ends.">>> a_string[3:11]‘alphabet' >>> a_string[3:-3]'alphabet starts where your alphabet en’ >>> a_string[:18]'My alphabet starts’ >>> a_string[18:]' where your alphabet ends.'
String VS. Bytes
Bytes對(duì)象的定義:b’ ’, eg: by = b’abcd/x65’
Bytes對(duì)象不能改變其值,但可以通過(guò)內(nèi)置函數(shù)bytearry()將bytes對(duì)象轉(zhuǎn)化成bytearry對(duì)象,bytearry對(duì)象的值可改變
>>> by = b'abcd/x65'>>> barr = bytearray(by)>>> barrbytearray(b'abcde') >>> barr[0]=102>>> barrbytearray(b'fbcde')
>>> a_string = "dive into python">>> by = a_string.encode('utf-8')>>> byb'dive into python'>>> roundtrip = by.decode('big5')>>> roundtrip'dive into python'
string.encode() -- 使用某種編碼方式作為參數(shù),將字符串轉(zhuǎn)化為bytes對(duì)象。
bytes.decode() -- 使用某種編碼方式作為參數(shù),將bytes對(duì)象轉(zhuǎn)化成字符串對(duì)象。
|
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注