python 使用随笔

    在使用python的过程中遇到很多问题,知识点比较散,通过这篇文件进行归纳。

    一、python的itertools模块

    combinations函数:将列表,按给定的长度进行组合。

from itertools import combinations
seq = [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 product
result1 = 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 requests


def get_data(url):
    s = requests.Session()
    s.auth = (username,  password)  #用户名和密码
    s.headers.update({'x-test': 'true'})
    res = s.get(url, headers={'x-test2': 'true'})    

 

posted @ 2018-04-07 11:12  tianrs  阅读(153)  评论(0编辑  收藏  举报