文件为json时间 201511 公司名称 Civitas Learning 公司名称链接 httpswwwitjuzicomcompany27639 轮次 C轮 金额 1620万美元 投资方 Rethink Education 最新估值估算 526亿人民币 字段1 教育 字段2 教育信息化 字段3 null 时间 201511 公司名称 Notesgen 公司名称链接 httpswwwitjuz
import json
读取数据
with open('data.json', 'r') as f: data = json.load(f)
定义函数,用于将金额转换为人民币
def convert_currency(amount, currency): if currency == '美元': return amount * 6.5 elif currency == '人民币': return amount else: return 0
输入需要统计的年份
year = input('请输入需要统计的年份:')
统计每个公司融资总次数和总金额
result = {} for d in data: if d['时间'].startswith(year): company = d['公司名称'] if company in result: result[company]['次数'] += 1 result[company]['金额'] += convert_currency(int(d['金额']), d['金额'][-2:]) else: result[company] = {'次数': 1, '金额': convert_currency(int(d['金额']), d['金额'][-2:])}
对结果进行排序
sorted_result = sorted(result.items(), key=lambda x: (-x[1]['次数'], -x[1]['金额'], x[0]))
将结果写入文件
with open('result.txt', 'w') as f: for r in sorted_result: f.write('{}\t{}\t{}\n'.format(r[0], r[1]['次数'], r[1]['金额'])
原文地址: https://www.cveoy.top/t/topic/fza7 著作权归作者所有。请勿转载和采集!