python例項:匯入會員資料後,讀取資料檔案,檢查匯入正確性(整列取excel值、合併列、response取值)
阿新 • • 發佈:2021-06-24
場景描述:某系統新上線,老系統的會員資料要匯入新系統中,包含手機號,上級資訊和會員餘額。有1萬多條資料,手工對比過於繁瑣,用python自動化處理
思路:系統匯入資料後,指令碼讀取檔案,先把會員資料取出來,合併成一個數列,然後傳入查詢函式,把查詢結果和讀取的資料進行比對,判斷匯入情況
資料檔案內容如下圖
系統查詢介面資訊如下:
第一步,讀取資料檔案,合併成新的數列。這一部分指令碼主要包含取excel中任意一列資料、迴圈讀取列資料傳到數列中、合併多個數列生成新數列
def phone(): wb = xlrd.open_workbook(path+'\\2021.xlsx')# 開啟Excel檔案 data = wb.sheet_by_name('sheet1')#通過excel表格名稱(rank)獲取工作表 data_1=data.col_values(0)#獲取第一列資料(陣列)#查詢號碼 data_2=data.col_values(1)#獲取第二列資料(陣列)#上級 data_3=data.col_values(2)#獲取第三列資料(陣列)#餘額 list1=[] list2=[] list3=[] for i in data_1[1:10]:#迴圈讀取列資料傳到數列中,[1:10]代表只讀取第2行到第10行資料list1.append(i) for h in data_2[1:10]: list2.append(h) for j in data_3[1:10]: list3.append(j) name_tulpe = list(zip(list1,list2,list3))#合併多個數列生成新數列 return(name_tulpe) code=phone()
列印輸出結果
第二步,合併後的新數列傳到請求函式中,逐一查詢匹配結果。這一部分指令碼主要用到數列的取值、資料拼接、request返回值json化後提取引數值
def data(params,headers,phone): for num in code: #分割資料,取出手機、上級、餘額(數列的取值) 手機 = int(num[0]) 上級 = str(num[1]) 餘額 = str(num[2]) print("查詢" + ' ' + str(手機)) print("匯入上級"+' '+上級) print("匯入餘額"+' '+餘額) #通過手機號查詢上級(資料拼接) data = '{"PageIndex":1,"PageSize":10,"KeyWord":'+ str(手機) +',"MemberType":"","CardIds":[],"MinLastBuyDate":"","MaxLastBuyDate":"","TagIds":[],"MinPoints":"","MaxPoints":"","MinCreateDate":"","MaxCreateDate":"","SortName":"CreateTime","Sort":"Desc","Source":0,"topAgentId":-1,"CustomProvId":0,"WorkFriendType":0,"ConcernGzhType":0,"p":0.6919700775737443}' response = requests.post('https://storeapi.xxx.com/xxx/Member/SearchMemberList', headers=headers, params=params, data=data) response = response.json() Id = response['Data']['DataList'][0]['Id']#客戶id FirstTwitterName = response['Data']['DataList'][0]['FirstTwitterName']#上級暱稱 #通過id查詢餘額(request返回值json化) res = requests.get('https://storeapi.xxx.com/xxx/Member/FindMemberInfoById?Id='+str(Id), headers=headers) res = res.json() Balance = res['Data']['Balance']#餘額(提取引數值) #列印上級和餘額資訊 print("查詢上級"+' '+FirstTwitterName) print("查詢餘額"+' '+str(Balance)) #判斷結果 if 上級 == FirstTwitterName and str(Balance) == 餘額: print("匹配成功") else: print("匹配失敗") print("---------------") data(params,headers,phone)
執行後的結果
有個問題,如果資料太多,第一步合併新數列會花費很多時間,這裡還需要尋找更好的方案,或者用jmeter實現
全部指令碼
# -*-coding:utf8-*- # encoding:utf-8 import requests import os import sys import xlrd path = os.path.abspath(os.path.dirname(sys.argv[0])) headers = { 'authority': 'storeapi.xxxxx.com', 'pragma': 'no-cache', 'cache-control': 'no-cache', 'access-control-request-method': 'POST', 'origin': 'https://adminstore.xxxx.com', 'user-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36', 'access-control-request-headers': 'authorization,content-type,req-host', 'accept': 'application/json', 'sec-fetch-site': 'same-site', 'sec-fetch-mode': 'cors', 'referer': 'https://adminstore.xxxx.com/', 'accept-encoding': 'gzip, deflate, br', 'accept-language': 'zh-CN,zh;q=0.9', 'req-host': 'adminstore.smallmitao.com', 'authorization': 'Bearer xxxx.xxxxx.eKlCnRt8PCC8qdfjS3rOuhAywaulFc3Ad2ujrmNo8uQ', 'content-type': 'application/json; charset=utf-8', } params = ( ('rend', '0.4068877130021684'), ) #讀取表格內的會員資料 def phone(): wb = xlrd.open_workbook(path+'\\2021.xlsx')# 開啟Excel檔案 data = wb.sheet_by_name('sheet1')#通過excel表格名稱(rank)獲取工作表 data_1=data.col_values(0)#獲取第一列資料(陣列)#查詢號碼 data_2=data.col_values(1)#獲取第二列資料(陣列)#上級 data_3=data.col_values(2)#獲取第三列資料(陣列)#餘額 list1=[] list2=[] list3=[] for i in data_1[1:10]:#迴圈讀取列資料傳到數列中,[1:10]代表只讀取第2行到第10行資料 list1.append(i) for h in data_2[1:10]: list2.append(h) for j in data_3[1:10]: list3.append(j) name_tulpe = list(zip(list1,list2,list3))#合併多個數列生成新數列 return(name_tulpe) code=phone() def data(params,headers,phone): for num in code: #分割資料,取出手機、上級、餘額 手機 = int(num[0]) 上級 = str(num[1]) 餘額 = str(num[2]) print("查詢" + ' ' + str(手機)) print("匯入上級"+' '+上級) print("匯入餘額"+' '+餘額) #通過手機號查詢上級 data = '{"PageIndex":1,"PageSize":10,"KeyWord":'+ str(手機) +',"MemberType":"","CardIds":[],"MinLastBuyDate":"","MaxLastBuyDate":"","TagIds":[],"MinPoints":"","MaxPoints":"","MinCreateDate":"","MaxCreateDate":"","SortName":"CreateTime","Sort":"Desc","Source":0,"topAgentId":-1,"CustomProvId":0,"WorkFriendType":0,"ConcernGzhType":0,"p":0.6919700775737443}' response = requests.post('https://storeapi.xxx.com/xxxx/Member/SearchMemberList', headers=headers, params=params, data=data) response = response.json() Id = response['Data']['DataList'][0]['Id']#客戶id FirstTwitterName = response['Data']['DataList'][0]['FirstTwitterName']#上級暱稱 #通過id查詢餘額 res = requests.get('https://storeapi.xxxx.com/xxxx/Member/FindMemberInfoById?Id='+str(Id), headers=headers) res = res.json() Balance = res['Data']['Balance']#餘額 #列印上級和餘額資訊 print("查詢上級"+' '+FirstTwitterName) print("查詢餘額"+' '+str(Balance)) #判斷結果 if 上級 == FirstTwitterName and str(Balance) == 餘額: print("匹配成功") else: print("匹配失敗") print("---------------") data(params,headers,phone)