如何将大型嵌套集拆分成单独的集?

How to split large nested set into separate sets?

您好,我有一组数据是从 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 = ["Capi总支出": "-13313000"...]

s_19_30_2019 = ["Capi总支出": "-10495000"...]

s_19_30_2020 = ["Capi总支出": "-7309000"...]

s_ttm = ["Capi总支出": "-9646000"...]

这样我就可以更轻松地访问数据并将它们添加到 sql 数据库中。

我尝试过 s_19_30_2018 = api['19/30/2018'] 但我一直在 'type error string indices must be integers '.

如有任何帮助,我们将不胜感激 python >.<

您的字典键是字符串,因此在访问它们时使用引号

s_19_30_2018 = api["9/30/2018"]

我在你的字典里也没有看到任何关键字,例如“19_30_2018”。

鉴于此结构:

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"] ...
ks = api.keys()
for k in ks:
  for key, value, in api[k]:
    print(f" {key}, {value}")

通过使用代码可以解决您的问题。

通过使用下面的代码.....你可以解决你的问题

ks = api.keys()
    for k in ks:
      for key, value, in api[k]:
        print(f" {key}, {value}")