標籤:result service rod 字元 請求 方式 repeat tool use
在使用python的過程中遇到很多問題,知識點比較散,通過這篇檔案進行歸納。
一、python的itertools模組
combinations函數:將列表,按給定的長度進行組合。
from itertools import combinationsseq = [1, 2, 3, 4]result = []for i in range(1, len(seq)+1): result.append(list(combinations(seq, i)))
結果:[(1,), (2,),(3,),(4,),(1,2),(1,3)...(1,2,3),(1,2,4), ...(1,2,3,4)]
product函數:
from itertools import productresult1 = list(product(‘abc‘, ‘xy‘))result2 = list(product(range(3), repeat=2))
result1:[(‘a‘, ‘x‘), (‘a‘, ‘y‘), (‘b‘, ‘x‘), (‘b‘, ‘y‘), (‘c‘, ‘x‘), (‘c‘, ‘y‘)]
result2:[(0,0), (0,1), (0,2), (1,0), (1,1), (1,2), (2,0), (2,1), (2,2)]
二、python調用SOA服務,使用suds模組
def testService(): url = ‘服務的wspl連結地址‘ client = suds.Client.Client(url) param = dict(x=‘123‘) result = client.service.METHOPNAME(param)METHOPNAME:來自於SOA服務函數名
三、python對列表中的字典元素進行排序
sort_list = [{‘datestamp‘: ‘2017-05‘}, {‘datestamp‘: ‘2017-04‘}]sort_list.sort(key=operator.itemgetter(‘datestamp‘))
四、python轉換字串編碼格式
_str = _str.encode(encoding=‘UTF-8‘, errors=‘strict‘)
五、請求資料模組requests
請求的url需要username和password時,可以採用如下方式:
import requestsdef get_data(url): s = requests.Session() s.auth = (username, password) #使用者名稱和密碼 s.headers.update({‘x-test‘: ‘true‘}) res = s.get(url, headers={‘x-test2‘: ‘true‘})
python 使用隨筆