Python的学习笔记案例8--空气质量指数计算2.0

本节课程主要讲:

1、读取已经获取的JSON数据文件

2、并将AQI前五的数据输出到JSON文件

什么是JSON文件?
JSON(JavaScript Object Notation)是一种轻量级数据交换格式,也是一种文本数据,可以使用文本格式打开。
可以对复杂数据进行表达和存储,易于阅读和理解。

JSON文件的规则:
1、数据保存在键值对中;
2、键值对之间有逗号隔开;
3、花括号用于保存键值对数据组成的对象;
4、方括号用于保存键值对数据组成的数组;(就是一个list列表)
采用对象、数组方式组织起来的键值对可以表示任何结构的数据。
JSON格式是互联网上主要使用的复杂数据格式之一。

下图就是一个JSON文件:

JSON库是处理JSON格式的Python标准库
两个过程:
1、编码(encoding),将Python数据类型转换成JSON格式的过程
2、解码(decoding),从JSON格式中解析数据对应到Python数据类型的过程

函数含义
dumps()将Python数据类型转换成JSON格式
loads()将JSON格式字符串转换为Python数据类型
dump()与dumps()函数功能一致,输出到文件
load()与loads()函数功能一致,从文件输入

以下为如何用Python如何从JSON文件获取数据,有事如何将数据输出到JSON文件中:

"""
    作者:lanxingbudui
    日期:2020-02-22
    功能:AQI计算
    功能2:如何输入JSON文件数据,如何输出JSON文件数据。
    版本:1.0
"""

import json


def process_json_file(filepath):
    """
        解码json文件
    """
    """
    处理文件三步走:1、打开文件;2、处理文件;;3、关闭文件
    """
    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)
    # 使用lambda函数 排序
    city_list.sort(key=lambda city: city['aqi'])
    top5_list = city_list[:5]

    # 输出到json文件
    f = open('top5_aqi.json', mode='w', encoding='utf-8')
    json.dump(top5_list, f, ensure_ascii=False)
    f.close()
    print(city_list)


if __name__ == '__main__':
    main()

最后加上beijing_aqi.json文件,复制一下,新建一个json文件即可。

[
   {
      "aqi":47,
      "area":"北京",
      "pm2_5":32,
      "pm2_5_24h":33,
      "position_name":"万寿西宫",
      "primary_pollutant":null,
      "quality":"优",
      "station_code":"1001A",
      "time_point":"2017-07-29T14:00:00Z"
   },
   {
      "aqi":63,
      "area":"北京",
      "pm2_5":37,
      "pm2_5_24h":20,
      "position_name":"定陵",
      "primary_pollutant":"颗粒物(PM10)",
      "quality":"良",
      "station_code":"1002A",
      "time_point":"2017-07-29T14:00:00Z"
   },
   {
      "aqi":57,
      "area":"北京",
      "pm2_5":40,
      "pm2_5_24h":36,
      "position_name":"东四",
      "primary_pollutant":"细颗粒物(PM2.5)",
      "quality":"良",
      "station_code":"1003A",
      "time_point":"2017-07-29T14:00:00Z"
   },
   {
      "aqi":44,
      "area":"北京",
      "pm2_5":24,
      "pm2_5_24h":30,
      "position_name":"天坛",
      "primary_pollutant":null,
      "quality":"优",
      "station_code":"1004A",
      "time_point":"2017-07-29T14:00:00Z"
   },
   {
      "aqi":46,
      "area":"北京",
      "pm2_5":28,
      "pm2_5_24h":38,
      "position_name":"农展馆",
      "primary_pollutant":null,
      "quality":"优",
      "station_code":"1005A",
      "time_point":"2017-07-29T14:00:00Z"
   },
   {
      "aqi":58,
      "area":"北京",
      "pm2_5":41,
      "pm2_5_24h":32,
      "position_name":"官园",
      "primary_pollutant":"细颗粒物(PM2.5)",
      "quality":"良",
      "station_code":"1006A",
      "time_point":"2017-07-29T14:00:00Z"
   },
   {
      "aqi":54,
      "area":"北京",
      "pm2_5":38,
      "pm2_5_24h":28,
      "position_name":"海淀区万柳",
      "primary_pollutant":"细颗粒物(PM2.5)",
      "quality":"良",
      "station_code":"1007A",
      "time_point":"2017-07-29T14:00:00Z"
   },
   {
      "aqi":102,
      "area":"北京",
      "pm2_5":76,
      "pm2_5_24h":38,
      "position_name":"顺义新城",
      "primary_pollutant":"细颗粒物(PM2.5)",
      "quality":"轻度污染",
      "station_code":"1008A",
      "time_point":"2017-07-29T14:00:00Z"
   },
   {
      "aqi":80,
      "area":"北京",
      "pm2_5":48,
      "pm2_5_24h":21,
      "position_name":"怀柔镇",
      "primary_pollutant":"臭氧1小时",
      "quality":"良",
      "station_code":"1009A",
      "time_point":"2017-07-29T14:00:00Z"
   },
   {
      "aqi":77,
      "area":"北京",
      "pm2_5":46,
      "pm2_5_24h":20,
      "position_name":"昌平镇",
      "primary_pollutant":"颗粒物(PM10)",
      "quality":"良",
      "station_code":"1010A",
      "time_point":"2017-07-29T14:00:00Z"
   },
   {
      "aqi":60,
      "area":"北京",
      "pm2_5":43,
      "pm2_5_24h":32,
      "position_name":"奥体中心",
      "primary_pollutant":"细颗粒物(PM2.5)",
      "quality":"良",
      "station_code":"1011A",
      "time_point":"2017-07-29T14:00:00Z"
   },
   {
      "aqi":50,
      "area":"北京",
      "pm2_5":35,
      "pm2_5_24h":27,
      "position_name":"古城",
      "primary_pollutant":null,
      "quality":"优",
      "station_code":"1012A",
      "time_point":"2017-07-29T14:00:00Z"
   },
   {
      "aqi":58,
      "area":"北京",
      "pm2_5":40,
      "pm2_5_24h":29,
      "position_name":null,
      "primary_pollutant":"颗粒物(PM2.5),颗粒物(PM10)",
      "quality":"良",
      "station_code":null,
      "time_point":"2017-07-29T14:00:00Z"
   }
]

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值