import pandas as pd
import requests
import json
def getAPIreturn(para):
url = "http://localhost/search?name={}".format(para)
try:
return_json = json.loads(requests.get(url).text)
response = return_json['data'] # get the first response string
print (type(response))
except:
response = ""
return response
data = pd.read_csv("input.csv")
data['return'] = data['para'].apply(getAPIreturn)
data.to_csv("output.csv",index=False)
上面的代码将读取一个 csv 文件并读取名为input调用 API 的第一列作为参数,然后将其data返回并保存到名为的第二列return并输出一个 CSV 文件。
如果我想从响应中选择多个值怎么办?例如,, ,score并将其保存回 CSV?countstatus
{
"data": [
{
"score": 1234
},
{
"count": 2
},
{
"id": "1234",
"Section": "abc",
"DisplayName": "hihi",
"status": "History"
}
]
}
csv 文件中的首选结果:
| 输入 | 分数 | 数数 | 地位 |
|---|---|---|---|
| 对 | 得分1 | 1 | 历史 |
| 第 2 段 | 得分2 | 2 | 历史 |