[英]How do I get the code to always select the latest file within a JSON file, then retrieve a specific value
我不想每季度/年更改代碼。 如何轉換我的 function 以便它簡單地選擇最新文件並檢索特定值 - 所以在下面的示例中,它將是 ['2020-12-31'] 被更改。 謝謝
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)
您可以將 json 中的值轉換為日期時間對象,然后取其中的最大值,例如:
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"]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.