標籤:
格式化字串/複合欄位名
>>> 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 是一個儲存當前python執行個體中搜有已匯入模組的字典。模組的名字為鍵,模組自身為值。
>>> 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=PapayaWhip‘] >>> a_list_of_list = [v.split(‘=‘,1) for v in a_list]>>> a_list_of_list[[‘user‘, ‘pilgrim‘], [‘database‘, ‘master‘], [‘password‘, ‘PapayaWhip‘]] >>> a_dict = dict(a_list_of_list)>>> a_dict{‘password‘: ‘PapayaWhip‘, ‘database‘: ‘master‘, ‘user‘: ‘pilgrim‘}
split()-根據指定的分隔字元,將字串分隔成一個字串列表。
dict() - 將包含列表的列錶轉換成字典對象
字串的分區
>>> 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對象的定義:b’ ’, eg: by = b’abcd\x65’
Bytes對象不能改變其值,但可以通過內建函數bytearry()將bytes對象轉化成bytearry對象,bytearry對象的值可改變
>>> 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() -- 使用某種編碼方式作為參數,將字串轉化為bytes對象。
bytes.decode() -- 使用某種編碼方式作為參數,將bytes對象轉化成字串對象。
Python學習筆記3-字串