我有復雜的 JSON 資料,我正在根據這些資料創建一個表。我可以清楚地獲得所有值。
但是如果所有資料都為零,我想洗掉或隱藏一行。就我而言,如果我的計數器是 5,那么我應該洗掉“標題”和“值”
我為此創建了一個函式,但我無法洗掉該行。我該怎么做?
for data1 in financial_balance:
for data2,key in data1.items():
if isinstance(key, str) != True:
for k in key:
for l,d in k.items():
if isinstance(d, str) != True:
for x in d:
count1 = 0
counter = 0
for e in x["values"]:
count1 = 1
if e["value"] == 0:
counter = 1
if counter == 5:
# remove row
我的資料:
[
{
'name': 'Balance Sheet',
'dataList': [
{
'mainEntry': 'Assets',
'titles': [
{
'title': 'Trade',
'values': [
{
'yea r': 2020,
'value': 268057,
'colorCode': None
},
{
'year': 2019,
'value': 421621,
'colorCod e': None
},
{
'year': 'Year over year trends 2019 vs 2020',
'value': -0.36,
'colorCode': None
},
{
'year': 'Common Size Analysis 2020',
'value': 0.12,
'colorCode': None
},
{
'year': 'Common Size Analysis 2019',
'value': 0.14,
'colorCode': None
}
]
},
{
'title': 'Inventories ',
'values': [
{
'year': 2020,
'value': 0,
'colorCode': None
},
{
'year': 2019,
'value': 0,
'colorCode': None
},
{
'year': 'Year over year trends 2019 vs 2020',
'value': 0,
'colorCod e': None
},
{
'year': 'Common Size Analysis 2020',
'value': 0,
'colorCode': None
},
{
'year': 'Common Size Analysis 2019',
'value': 0,
'colorCode': None
}
]
}
},
uj5u.com熱心網友回復:
您可以使用del關鍵字從字典中洗掉字典鍵。例如,del x[e]["value"]。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/452088.html
下一篇:使用顛簸變換截斷字串的一部分
