一、字符串与列表转换:
1、字符串转列表(分隔符截取字符串):split
time_set = "0 0/3 9-11 * * ?" print(time_set.split(' '))
输出:['0', '0/3', '9-11', '*', '*', '?']
2、列表转字符串:join
list = ['1', '2', '3', '4', '5', '6'] print(','.join(list))
输出:1,2,3,4,5,6
二、json与字典转换:
1、接口请求获取的数据转列表(转字典、列表或元组都可用json.loads)
import json resp = requests.get(url, headers=headers) jobdata = json.loads(resp.text) for item in jobdata: print(item['jobName'])
输出:
LoanExpireSendSmsJob
LoanOverSendSmsJob
2、Python数据结构转换为JSON:json.dumps
import json url = server_id('host_new')+'/jrocket2/api/h5/profile' data = {"name":self.name,"cnid":self.cnid} headers = {'content-type': 'application/json', 'X-Product-Code': self.X_Product_Code,'X-User-Token': self.token} resp = requests.post(url, data=json.dumps(data),headers=headers)