In my current code I'm iterating through 4500 cell values of a column from an excel file. In each iteration, the cell value is added between two halves of an invalid URL to form a valid URL. I can then request data from the URL for that specific cell value. How can I append the datasets for each cell value to ONE list in 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)
Current Output for 2 iterations:
[[{'Fruit': 'Apple', 'Protein': 'Steak', 'Vegetable': 'Cabbage' }]]
[[{'Fruit': 'Pear', 'Protein': 'Chicken', 'Vegetable': 'Spinach'}]]
DESIRED OUTPUT
[
[{'Fruit': 'Apple', 'Protein': 'Steak', 'Vegetable': 'Cabbage' }],
[{'Fruit': 'Pear', 'Protein': 'Chicken', 'Vegetable': 'Spinach'}],
]
CodePudding user response:
You should move the alist
outside the for-loop like below:
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)