I don't want to change the code each quarter/year. How can I transform my function so it simply selects the latest file and retrieves a specific value - so in the example below, it would be the ['2020-12-31'] that is altered. Thank you
def freeCashFlow():
for r in range(len(cleanstockdata2)):
try:
if (cleanstockdata2[r]['Financials']['Cash_Flow']['yearly']['2020-09-30']["freeCashFlow"]) in ['0','',None]:
print (0)
else:
print (cleanstockdata2[r]['Financials']['Cash_Flow']['yearly']['2020-09-30']["freeCashFlow"])
except KeyError:
print (0)
You can convert the values in the json to datetime objects and just take the max of them, for instance:
from datetime import datetime as dt
date_list = [dt.strptime(key, '%Y-%m-%d') for key in cleanstockdata2[r]['Financials']['Cash_Flow']['yearly'].keys()]
max_date = dt.strftime(max(date_list), '%Y-%m-%d')
desired_value = cleanstockdata2[r]['Financials'][max_date]["freeCashFlow"]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.