Page 1 of 1

PYTHON HELP: read table and code first import openpyxl import json import requests import pandas as pd path = 'C:\\Users

Posted: Mon Jun 06, 2022 6:08 pm
by answerhappygod
PYTHON HELP: read table and code first
import openpyxl
import json
import requests
import pandas as pd
path = 'C:\\Users\\moses\\Desktop\\Program
Projects\\merger\\allstockdata.xlsx'
wb = openpyxl.load_workbook(path)
ws = wb['completestocktable']
#Iterating through the values(Stock Ticker Symbols) in Column
A
for cell in ws['A']:
#relevant metrics that can be found in the data
requested from the API url at line 18
keys = ['symbol', 'period', 'priceToSalesRatio',
'debtToEquity', 'pbRatio', 'revenuePerShare', 'freeCashFlowYield',
'roic', 'roe', 'netIncomePerShare', 'debtToAssets', 'peRatio',
'researchAndDdevelopementToRevenue', 'netIncomeGrowth',
'epsgrowth', 'revenueGrowth', 'freeCashFlowGrowth']
datadict = {}
#adding a cell value between 2 halves of an
incomplete API URL forms a valid url that data can be requested
from
alldata =
json.loads(requests.get('https://financialmodelingprep.com/api/v3/key-metrics/'
+ cell.value +
'?period=quarter&limit=130&apikey=d32d97c8217d40ed4dd4118e8dd6dbff').text)
+
json.loads(requests.get('https://financialmodelingprep.com/api/v ... al-growth/'
+ cell.value +
'?period=quarter&limit=80&apikey=d32d97c8217d40ed4dd4118e8dd6dbff').text)

if len(alldata) == 0: #No data on a stock in the
API
alldata = None
else:
for a in range(len(alldata)):
for key, val in
alldata[a].items():
if key in
keys:

datadict[key] = val #A dictionary with keys and the
values respective to the stock ticker(cell.value)
# Per iteration through the cell
values of Column A, there should be a dictionary of metrics for
that cell value (stock ticker) Feel free to print(datadict)
to see the dictionary of sample data
CURRENT PROBLEM
1. I would like to append each datadict from an
iteration to one list until the program has iterated through
all cell values of Column A of the xlsx file. For example I would
like my list after iteration to look like:
onelist = [datadict, datadict, datadict]
SAME THING AS [{'key': 'value'}, {'key': 'value'},
{'key': 'value'}]
2. I would like to write a new xlsx file using the list
of dictionaries (datadict iterations). In that excel file I would
like the keys in the dictionary of the list to be the Columns and
the values of those keys to be the cell.values in those columns
respectively.
Please help code with this and let me know if something
is misunderstood
#specifying the data metrics from the
API urls that are important in a list