代码拉取完成,页面将自动刷新
同步操作将从 Yiman/VisualizationProject 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
from json import load
"""
描述:数据模块
功能:清洗关于疫情的数据,并提供接口
"""
# 清洗不规范名称
def format_name_of_province(province_name):
return province_name.replace('自治区', '').replace('壮族', '').replace('回族', '').replace('维吾尔', '')
class EpidemicSituation:
def __init__(self):
self.cure_rate = 0
self.death_rate = 0
self.danger_areas = []
self.danger_city_name = []
self.suspected_city_name = []
self.suspected_count = []
self.suspected_count_geo = []
self.serious_count = []
self.suspected_count_currency = []
self.current_confirm_nums = []
self.current_serious_nums = []
self.current_confirm_nums_with_city = []
self.current_serious_nums_with_city = []
self.city_name_list = []
self.danger_city_value = []
self.danger_city_high_level = []
self.cov_nums = {}
self.cov_date = []
self.current_confirm = []
self.danger_city_mid_level = []
self.format_data()
self.count_cov_nums()
self.cal_rate()
self.count_suspected_data()
def count_detail_info_of_danger_areas(self, danger_areas):
mid_level_count = 0
high_level_count = 0
for i in danger_areas:
if i["dangerLevel"] == 2:
mid_level_count += 1
continue
high_level_count += 1
self.danger_city_high_level.append(high_level_count)
self.danger_city_mid_level.append(mid_level_count)
def count_cov_nums(self):
with open('./data/cov_num.json', 'r', encoding='utf-8') as f:
cov_data = load(f)
self.cov_nums = cov_data[-1]
for i in range(0, len(cov_data)):
self.suspected_count.append(cov_data[i]['suspectedCount'])
self.cov_date.append(cov_data[i]['date'])
self.current_confirm.append(cov_data[i]['currentConfirmedCount'])
self.serious_count.append(cov_data[i]['seriousCount'])
def format_data(self):
with open('./data/cov_info_of_china.json', 'r', encoding='utf-8') as fp:
json_data = load(fp)
file_len = len(json_data)
for i in range(0, file_len):
lengthy_of_danger = len(json_data[i]['dangerAreas']) # dangerAreas信息存放在列表,通过长度判断是否为空
current_num = json_data[i]['currentConfirmedCount']
serious_num = json_data[i]['suspectedCount']
city_name = format_name_of_province(json_data[i]['provinceName']) # 格式化省份名称,避免地图上画不出
self.city_name_list.append(city_name)
# 清洗好的数据放在列表里面
if lengthy_of_danger != 0:
self.danger_city_value.append(lengthy_of_danger)
self.danger_city_name.append(city_name)
self.count_detail_info_of_danger_areas(json_data[i]['dangerAreas'])
if current_num != 0:
self.current_confirm_nums.append(current_num)
if serious_num != 0:
self.current_serious_nums.append(serious_num)
self.danger_areas = list(zip(self.danger_city_name, self.danger_city_value))
self.current_confirm_nums_with_city = list(zip(self.current_confirm_nums, self.city_name_list))
self.current_serious_nums_with_city = list(zip(self.city_name_list, self.current_serious_nums))
self.current_confirm_nums_with_city.sort(reverse=True) # 降序排列
# 清洗比较繁琐,单独拎出来写
def count_suspected_data(self):
with open('./data/cov_info_of_china.json', 'r', encoding='utf-8') as fp:
json_data = load(fp)
file_len = len(json_data)
count = 0
for i in range(0, file_len):
lengthy_of_cities = len(json_data[i]['cities'])
if lengthy_of_cities == 0:
continue
j = 0
while j < lengthy_of_cities:
city_dict = json_data[i]['cities'][j]
if city_dict['cityName'] != '境外输入':
j += 1
continue
elif city_dict['currentConfirmedCountStr'].isdigit() is False:
j += 1
continue
elif eval(city_dict['currentConfirmedCountStr']) == 0:
j += 1
continue
else:
count += eval(city_dict['currentConfirmedCountStr'])
self.suspected_city_name.append(self.city_name_list[i])
self.suspected_count_geo.append(eval(city_dict['currentConfirmedCountStr']))
break
def count_suspected_currency(self):
currency = []
lengthy = len(self.suspected_count)
i = 0
while i < lengthy - 1:
tem = self.suspected_count[i + 1] - self.suspected_count[i]
if tem < 0:
tem = 0
currency.append(tem)
i += 1
return currency
def count_serious_currency(self):
currency = []
lengthy = len(self.serious_count)
i = 0
while i < lengthy - 1:
currency.append(self.serious_count[i + 1] - self.serious_count[i])
i += 1
return currency
def cal_rate(self):
self.cure_rate = self.cov_nums['cureCount'] / self.cov_nums['confirmedCount']
self.death_rate = self.cov_nums['deadCount'] / (self.cov_nums['deadCount'] + self.cov_nums['cureCount'])
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。