将数据附加到JSON中

2024-04-26 14:16:56 发布

您现在位置:Python中文网/ 问答频道 /正文

嗨,我从excel中使用查询字符串获取url输入,并将其运行到循环中,从url获得响应,然后将数据转储到json文件中,但问题是它只返回最后10行的结果,所以它没有在这里附加整个输出是代码。你知道吗

import pandas as pd
import requests
import json
from pandas.io.json import json_normalize
from flatten_json import flatten

df=pd.read_excel('C:/Users/cust.xlsx',index=None,index_col=None,encoding='UTF-8')
df.head(5)

df=df[['Name','OU']]
df.head(5)

passwd=b"password"
user=b"abcd"
# Make a request to the endpoint using the correct auth values
auth_values = (user,passwd)
dd=df.values
for i,j in dd:
    url='http://xyz.co/&name='+str(i)+'&countryCode='+str(j)  
    resp = requests.get(url,auth=auth_values)

resp.json()
cust = pd.io.json.json_normalize(resp.json())

cust.dtypes
cust.head(2)
cust.customer.head(10)

cust.customer.apply(lambda customer: len(customer)).value_counts()

customer = pd.io.json.json_normalize(resp.json(), record_path='customer')
customer.head()
df=customer.to_excel('C:/Users/cm.xlsx',index=False)

Tags: ioimportauthjsonurldfindexcustomer