【发布时间】:2021-12-05 09:32:06
【问题描述】:
您好,我有一组从 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 中的任何帮助将不胜感激>。
【问题讨论】:
-
做
s_19_30_2018 = api["9/30/2018"]好像有效,你确定`api` 是字典吗? -
这里没有设置。
-
@DaniMesejo 我试过了,但我不断收到 s_18 = api["9/30/2018"] TypeError: string indices must be integers
-
那是因为 api 不是字典,是字符串。或者错误是指代码的其他部分
-
不需要像
s_19_30_2019这样的变量。如果您的数据集是api,那么请参考api["9/30/2019"]而不是s_19_30_2019。您要求的是创建动态命名的变量。但是在 Python 中,这样做的方法是使用你已经拥有的字典。如果执行api["9/30/2019"]会为您提供有关字符串索引的错误消息,则数据与您描述的不同,因为这表明api是一个字符串,而您的问题将其显示为字典。如果此数据是 JSON,则查看json模块将其转换为字典。
标签: python arrays list dictionary set