python空⽓质量指数计算_Python⼊门案例(⼋):空⽓质量
指数(AQI)计算
command键盘是哪个
⼀、空⽓质量指数计算V1.0
主要知识点:分⽀结构、函数、异常处理# -*- coding:utf-8 -*-
"""
@author:Angel
@file:AQI_V1.0.py
@time:2018/11/28 14:29
@1.0功能:AQI计算
"""
def cal_linear(iaqi_lo, iaqi_hi, bp_lo, bp_hi, cp):
# 线性缩放
iaqi = (iaqi_hi - iaqi_lo) * (cp - bp_lo) / (bp_hi - bp_lo) +iaqi_lo
return iaqi
def cal_pm_iaqi(pm_val):
# 计算PM2.5的IAQI
if 0 <= pm_val < 36:
iaqi = cal_linear(0, 50, 0, 35, pm_val)
elif 36 <= pm_val < 76:
iaqi = cal_linear(50, 100, 35, 75, pm_val)
elif 76 <= pm_val < 116:
iaqi = cal_linear(100, 150, 75, 115, pm_val)
elif 116 <= pm_val < 151:
iaqi = cal_linear(150, 200, 115, 150, pm_val)
elif 151 <= pm_val < 251:
iaqi = cal_linear(200, 300, 150, 250, pm_val)
elif 251 <= pm_val < 351:
iaqi = cal_linear(300, 400, 250, 350, pm_val)
elif 351 <= pm_val < 501:
iaqi = cal_linear(400, 500, 350, 500, pm_val)
else:
pass
return iaqi
def cal_co_iaqi(co_val):
# 计算co的IAQI
if 0 <= co_val < 3:
iaqi = cal_linear(0, 50, 0, 2, co_val)
elif 3 <= co_val < 5:
iaqi = cal_linear(50, 100, 2, 4, co_val)
elif 5 <= co_val < 15:
iaqi = cal_linear(100, 150, 4, 14, co_val) elif 15 <= co_val < 25:
iaqi = cal_linear(150, 200, 14, 24, co_val) elif 25 <= co_val < 37:
iaqi = cal_linear(200, 300, 24, 36, co_val) elif 37 <= co_val < 49:
iaqi = cal_linear(300, 400, 36, 48, co_val) elif 49 <= co_val < 61:
iaqi = cal_linear(400, 500, 48, 60, co_val) else:
pass
return iaqi
def cal_aqi(param_list):
# AQI计算
pm_val = param_list[0]
co_val = param_list[1]
pm_iaqi = cal_pm_iaqi(pm_val)
co_iaqi = cal_co_iaqi(co_val)
iaqi_list = []
iaqi_list.append(pm_iaqi)
iaqi_list.append(co_iaqi)
AQI = max(iaqi_list)
return AQI
def main():
print('请输⼊以下信息,⽤空格分隔:')
input_str = input('(1)PM2.5 (2)co:')
str_list = input_str.split(' ')
pm_val = float(str_list[0])
co_val = float(str_list[1])
param_list = []
param_list.append(pm_val)
param_list.append(co_val)
# 调⽤AQI计算函数
aqi_val = cal_aqi(param_list)
print('空⽓质量指数(AQI):{}'.format(aqi_val))
if __name__ == '__main__':
main()
css外部样式表
⼆、空⽓质量指数计算V2.0
主要知识点:JSON⽂件格式及操作:JSON是⼀种轻量级数据交换格式,可以对复杂数据进⾏表达和存储,易于阅读和理解。数据保存在键值对中,键值对之间由逗号分隔,花括号⽤于保存键值对数据组成的对象,⽅括号⽤户保存键值对数据组成的数组;采⽤对象、数组⽅式组织起来的键值对可以表⽰任何结构的数据JSON库是处理JSON数据格式的Python标准库,两个过程:编码(encoding),将Python数据类型转换成JSON格式的过程;解码(decoding),从JSON格式中解析数据对应到Python数据类型的过程# -*- coding:utf-8 -*-
"""
@author:Angel
@file:AQI_V2.0.py
@time:2018/11/28 14:29
@1.0功能:AQI计算
@2.0功能:读取已经获取的JSON数据⽂件,并将AQI前5的数据输出到⽂件
"""
import json
def process_json_file(filepath):
# 解码JSON⽂件
f = open(filepath, mode='r', encoding='utf-8')
city_list = json.load(f)
return city_list
def main():
filepath = input('请输⼊JSON⽂件名称:')
city_list = process_json_file(filepath)
city_list.sort(key=lambda city: city['aqi'])
top5_list = city_list[:5]
f = open('top5_aqi.json', mode='w', encoding='utf-8')
json.dump(top5_list, f, ensure_ascii=False)
f.close()
if __name__ == '__main__':
main()
免费风景视频素材三、空⽓质量指数计算V3.0
主要知识点:CSV是⼀种通⽤的、相对简单的⽂件格式,以⾏为单位,每⾏表⽰⼀条记录,以英⽂逗号分隔每列数据,列名通常放置在⽂件第⼀⾏# -*- coding:utf-8 -*-
"""
@author:Angel
@file:AQI_V3.0.py
@time:2018/11/28 14:29
@1.0功能:AQI计算
@2.0功能:读取已经获取的JSON数据⽂件,并将AQI前5的数据输出到⽂件
@3.0功能:读取已经获取的JSON数据⽂件,并将其转换成CSV⽂件
"""
import json
import csv
def process_json_file(filepath):
# 解码JSON⽂件
f = open(filepath, mode='r', encoding='utf-8')
web30是什么意思city_list = json.load(f)
return city_list
def main():
filepath = input('请输⼊JSON⽂件名称:')
city_list = process_json_file(filepath)
city_list.sort(key=lambda city: city['aqi'])
lines = []
# 列名
lines.append(list(city_list[0].keys()))
# 数据
for city in city_list:
lines.append(list(city.values()))
f = open('aqi.csv', 'w', encoding='utf-8', newline='')
writer = csv.writer(f)
for line in lines:
writer.writerow(line)
f.close()
if __name__ == '__main__':
main()
四、空⽓质量指数计算V4.0
主要知识点:os模块:提供了与系统、⽬录操作相关的功能,不受平台的限制使⽤with语句操作⽂件对象,不管在处理⽂件过程中是否发⽣异常,都能保证with语句执⾏完毕后关闭⽂件,不需要close()语句# -*- coding:utf-8 -*-
"""
@author:Angelword教程视频免费
@file:AQI_V4.0.py
@time:2018/11/28 14:29
@1.0功能:AQI计算
@2.0功能:读取已经获取的JSON数据⽂件,并将AQI前5的数据输出到⽂件
@3.0功能:读取已经获取的JSON数据⽂件,并将其转换成CSV⽂件
@4.0功能:根据输⼊的⽂件判断是JSON格式还是CSV格式,并进⾏相应的操作
"""
import json
import csv
import os
def process_json_file(filepath):
# 处理JSON⽂件
with open(filepath, mode='r', encoding='utf-8') as f:
city_list = json.load(f)
print(city_list)
def process_csv_file(filepath):
# 处理CSV⽂件
with open(filepath, mode='r', encoding='utf-8', newline='') as f:
reader = ader(f)
for row in reader:
python请求并解析json数据
print(', '.join(row))
def main():
filepath = input('请输⼊⽂件名称:')
filename, file_ext = os.path.splitext(filepath)
if file_ext == '.json':

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。