在线教育市场投资数据分析:2015-2019年每月投资事件统计
import json
from collections import defaultdict
# 读取json文件
with open('data.json', 'r', encoding='utf-8') as f:
data = json.load(f)
month = '06' # 指定月份
result = defaultdict(lambda: defaultdict(lambda: {'count': 0, 'amount': 0})) # 初始化结果字典
# 遍历数据,统计每年在指定月份的投资次数和投资金额
for item in data:
date = item['时间']
year = date.split('/')[0]
if date.endswith('/' + month) and year in ['2015', '2016', '2017', '2018', '2019']:
count = result[year]['count']
amount = result[year]['amount']
if item['金额'] != '未披露': # 已知金额
if '万元' in item['金额']: # 单位为万元
amount += float(item['金额'].strip('万元')) * 10000
elif '美元' in item['金额']: # 单位为美元
amount += float(item['金额'].strip('美元')) * 6.5
else: # 单位为人民币
amount += float(item['金额'].strip('亿人民币')) * 100000000
count += 1
result[year]['count'] = count
result[year]['amount'] = amount
# 按照投资次数降序输出,次数相同按照金额降序,金额也相同按照年份升序
res = []
for year in result:
count = result[year]['count']
amount = result[year]['amount']
res.append((year, count, amount))
res.sort(key=lambda x: (-x[1], -x[2], x[0]))
# 将结果写入result.txt文件中
with open('result.txt', 'w', encoding='utf-8') as f:
for r in res:
f.write(f'{r[0]}年{month}月投资次数:{r[1]},投资金额:{r[2]:.2f}元\n')
原文地址: https://www.cveoy.top/t/topic/oiXK 著作权归作者所有。请勿转载和采集!