import json
from operator import itemgetter
from itertools import groupby

files=r'D:\hg\3.json'
# 读取JSON文件
with open(files, 'r') as file:
    data = json.load(file)

data=data['facts']['us-gaap']
zb=[]
# 遍历字典的key值
for key in data.keys():
    accounts=data[key]['units']
    for kk in accounts.keys():
        accounts=accounts[kk]
        for account in accounts:
            end=str(account['end'])
            val=str(account['val'])
            fp=str(account['fp'])
            form=str(account['form'])
            zhibiao={
               'zbname':key,
               'riqi':end,
               'jine':val,
               'fp':fp,
               'form':form,
            }
            zb.append(zhibiao)
#找10-K的数据
# 根据age属性对列表内容进行分类
zb.sort(key=itemgetter('form'))  # 先按照age属性进行排序
forms = groupby(zb, key=itemgetter('form'))  # 根据age属性进行分组

form_25_data = []
for form, zz in forms:
    if '10-K' in form :
        form_25_data.extend(list(zz))

# 根据age属性对列表内容进行分类
form_25_data.sort(key=itemgetter('riqi'))  # 先按照age属性进行排序
groups = groupby(form_25_data, key=itemgetter('riqi'))  # 根据age属性进行分组
# 遍历每个分组，并打印分类结果
for riqi, group in groups:
    print(f"riqi: {riqi}")
    for item in group:
        print(item)
    print()






