在我當前的代碼中,我正在遍歷 excel 檔案中列的 4500 個單元格值。在每次迭代中,將單元格值添加到無效 URL 的兩半之間以形成有效 URL。然后,我可以從 URL 請求該特定單元格值的資料。如何將每個單元格值的資料集附加到 Python 中的一個串列?
import json
import requests
import pandas as pd
data = pd.read_excel('allstockdata.xlsx')
look = list(data["Meals"])
for i in look:
alist = [] #Line of Interest
i = str(i)
alldata = json.loads(requests.get(url1half1 i url1half2).text) json.loads(requests.get(url2half1 i url2half2).text) #Gets data in form of single nested dictionary in list for each iteration [{}]
alist.append(alldata) # Line of Interest
print(alist)
2 次迭代的電流輸出:
[[{'Fruit': 'Apple', 'Protein': 'Steak', 'Vegetable': 'Cabbage' }]]
[[{'Fruit': 'Pear', 'Protein': 'Chicken', 'Vegetable': 'Spinach'}]]
期望的輸出
[
[{'Fruit': 'Apple', 'Protein': 'Steak', 'Vegetable': 'Cabbage' }],
[{'Fruit': 'Pear', 'Protein': 'Chicken', 'Vegetable': 'Spinach'}],
]
uj5u.com熱心網友回復:
您應該alist像下面這樣移動 for 回圈的外部:
import json
import requests
import pandas as pd
data = pd.read_excel('allstockdata.xlsx')
look = list(data["Meals"])
alist = [] #Line of Interest
for i in look:
i = str(i)
alldata = json.loads(requests.get(url1half1 i url1half2).text) json.loads(requests.get(url2half1 i url2half2).text) #Gets data in form of single nested dictionary in list for each iteration [{}]
alist.append(alldata) # Line of Interest
print(alist)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/489165.html
下一篇:在python中同時迭代多個串列
