python 使用隨筆

來源:互聯網
上載者:User

標籤: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 使用隨筆

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.