根据数据框结果从API中提取数据?

时间:2019-06-25 18:16:34

标签: python api dataframe

我以前运行过SQL,并将结果存储在python代码内的数据框中。我现在正试图找出如何根据数据框的结果从API中提取数据的地方,

数据帧'PID / SKU'= API plSkuSet下的第二层

数据帧'GPL_PL'= API plSkuSet下的第一层

目前,我可以从API的“全局”下获取下面的3个示例项(ex1,ex2,ex3),但是我试图根据数据帧结果弄清楚如何使它们动态化。我尝试过的一切都会给我带来错误

import requests
import json
from requests.auth import HTTPBasicAuth
import pandas as pd

pidList = []
gplFactorLIST = []
wplFactorLIST = []
errorCodeLIST = []

headers = {'Content-type': 'application/json'}
pids = ["EX1","EX2","EX3"]
data = {"transactionId": 
"b60545f6a3ff0dd2:4eb0da1f:15e8fd6ba10:-7ffd77909","plSkuSet": 
{"Global": pids }}
resp = requests.post('APIURLTEST', json=data,headers=headers,  
auth=HTTPBasicAuth('user3939.gen', 'Password'))

if resp.status_code == 200:
    resp_json = resp.json()
    pricelist_data = resp_json['plSkuFactorDetails']['Global']


    for pid in pids:
         pidList.append(pid)
        try:

            gplFactorLIST.append(pricelist_data[pid]['gplFactor'])
             wplFactorLIST.append(pricelist_data[pid]['wplFactor'])
            errorCodeLIST.append(pricelist_data[pid]['errorCode'])
        except KeyError:

            gplFactorLIST.append('No Data')
            wplFactorLIST.append('No Data')
            errorCodeLIST.append('No Data')

    df = pd.DataFrame({'Pid': pidList, 'gplFactor': 
gplFactorLIST,'wplFactor': wplFactorLIST, 'errorCode': 
errorCodeLIST})
    print(df)
else:
    print('Error in Api')

0 个答案:

没有答案