嗨,我有一組從 api 中提取的資料,我試圖將集合中的資料拆分為單獨的集合,因為目前它們都嵌套在較大的集合中。
我目前的套餐:
api = {
"9/30/2018": {
"Capital Expenditure": "-13313000",
"End Cash Position": "25913000",
"Financing Cash Flow": "-87876000",
"Free Cash Flow": "64121000",
"Income Tax Paid Supplemental Data": "10417000",
"Interest Paid Supplemental Data": "3022000",
"Investing Cash Flow": "16066000",
"Issuance of Capital Stock": "669000",
"Issuance of Debt": "6969000",
"Operating Cash Flow": "77434000",
"Repayment of Debt": "-6500000",
"Repurchase of Capital Stock": "-72738000"
},
"9/30/2019": {
"Capital Expenditure": "-10495000",
"End Cash Position": "50224000",
"Financing Cash Flow": "-90976000",
"Free Cash Flow": "58896000",
"Income Tax Paid Supplemental Data": "15263000",
"Interest Paid Supplemental Data": "3423000",
"Investing Cash Flow": "45896000",
"Issuance of Capital Stock": "781000",
"Issuance of Debt": "6963000",
"Operating Cash Flow": "69391000",
"Repayment of Debt": "-8805000",
"Repurchase of Capital Stock": "-66897000"
},
"9/30/2020": {
"Capital Expenditure": "-7309000",
"End Cash Position": "39789000",
"Financing Cash Flow": "-86820000",
"Free Cash Flow": "73365000",
"Income Tax Paid Supplemental Data": "9501000",
"Interest Paid Supplemental Data": "3002000",
"Investing Cash Flow": "-4289000",
"Issuance of Capital Stock": "880000",
"Issuance of Debt": "16091000",
"Operating Cash Flow": "80674000",
"Repayment of Debt": "-12629000",
"Repurchase of Capital Stock": "-72358000"
},
"ttm": {
"Capital Expenditure": "-9646000",
"End Cash Position": "35276000",
"Financing Cash Flow": "-94328000",
"Free Cash Flow": "94768000",
"Income Tax Paid Supplemental Data": "19627000",
"Interest Paid Supplemental Data": "2597000",
"Investing Cash Flow": "-9849000",
"Issuance of Capital Stock": "1011000",
"Issuance of Debt": "22370000",
"Operating Cash Flow": "104414000",
"Repayment of Debt": "-7500000",
"Repurchase of Capital Stock": "-83410000"
}
}
我想要的結果是:s_19_30_2018 = [“資本支出”:“-13313000”...]
s_19_30_2019 = [“資本支出”:“-10495000”...]
s_19_30_2020 = [“資本支出”:“-7309000”...]
s_ttm = [“資本支出”:“-9646000”...]
這樣我就可以更輕松地訪問資料并將它們添加到 sql 資料庫中。
我已經嘗試過 s_19_30_2018 = api['19/30/2018'] 但我不斷收到“型別錯誤字串索引必須是整數”。
任何幫助將不勝感激在 python >.<
uj5u.com熱心網友回復:
鑒于這種結構:
api = {
"9/30/2018": {
"Capital Expenditure": "-13313000",
"End Cash Position": "25913000",
},
"9/30/2019": {
....
要獲取鍵串列,可以運行第一個條目的值:
for key,value in api["9/30/2018"]:
l = [key, value]
print(f" {key}, {value}")
# prints "Capital Expenditure -133313000"
瀏覽所有專案
#get the keys
ks = api.keys()
for k in ks:
for key, value, in api[k]:
print(f" {key}, {value}")
# prints "Capital Expenditure -133313000"] ...
uj5u.com熱心網友回復:
您的字典鍵是字串,因此在訪問它們時使用引號
s_19_30_2018 = api["9/30/2018"]
我也沒有在您的字典中看到任何鍵,例如“19_30_2018”。
uj5u.com熱心網友回復:
ks = api.keys()
for k in ks:
for key, value, in api[k]:
print(f" {key}, {value}")
通過使用代碼,您可以解決您的問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/322292.html
上一篇:使用Linq根據條件過濾物件串列
下一篇:我正在努力理解以下串列理解
