我正在從API中獲取信息。返回的數據是JSON格式。我必須迭代並獲取多個輸入的相同數據。我想將每個輸入的JSON數據保存在python字典中以便於訪問。這是我到目前爲止有:如何讀取API JSON數據並存儲爲Python字典
import pandas
import requests
ddict = {}
read_input = pandas.read_csv('input.csv')
for d in read_input.values:
print(d)
url = "https://api.xyz.com/v11/api.json?KEY=123&LOOKUP={}".format(d)
response = requests.get(url)
data = response.json()
ddict[d] = data
df = pandas.DataFrame.from_dict(ddict, orient='index')
with pandas.ExcelWriter('output.xlsx') as w:
df.to_excel(w, 'output')
與上面的代碼,我得到以下的輸出:
a.com
我也只有從這個第一線獲得與數據的Excel輸出。我的輸入CSV文件有近400行,所以我應該看到在輸出和輸出excel文件中多於1行。
如果您有更好的方法來做到這一點,將不勝感激。另外,我得到的excel輸出很難理解。我想用字典和子字典讀取JSON數據,但我不完全理解底層數據的格式 - 我認爲它看起來最接近JSON數組。
我看了很多其他帖子,包括Parsing values from a JSON file using Python?和How do I write JSON data to a file in Python?和Converting JSON String to Dictionary Not List和How do I save results of a "for" loop into a single variable?,但迄今爲止這些技術都沒有工作。如果可能的話,我寧願不醃。
我是新來的Python,所以任何幫助表示讚賞!
我做了編輯,但代碼仍然沒有迭代通過csv文件。 – SizzyNini
你應該迭代'read_input.values'。 –