Mieux_2022 2023-06-05 10:01 采纳率: 75%
浏览 39
已结题

Python提取json数据并存入csv,提取json文件内容信息报错

代码

import json
import csv

# 打开JSON文件并加载数据
with open('4_.json', 'rb') as f:
    data_list = json.load(f)
    arr = data_list['data']
    print(arr['list'])
    for data in arr:
        # 提取停车场信息
        mchname = data['list']['mchname']
        parkname = data['list']['parkname']
        totalspace = data['list']['totalspace']
        usedspace = data['list']['usedspace']
        freeMinute = data['list']['freeMinute']

# 将信息写入CSV文件
with open('parking_info2.csv', 'w', newline='') as f:
    writer = csv.writer(f)
    writer.writerow(['运营公司', '停车场名称', '总车位数', '当前使用车位数','免费时长'])
    writer.writerow([mchname, parkname, totalspace, usedspace,freeMinute])

报错信息

Traceback (most recent call last):
  File "F:\text1\爬虫\pc4.py", line 11, in <module>
    mchname = data['list']['mchname']
TypeError: string indices must be integers

json文件

{
    "status":1,
    "msg":"success",
    "data":{
        "total":153,
        "offset":0,
        "limit":0,
        "list":[
            {
                "id":173,
                "parkid":"1-01-2-3019",
                "parentid":null,
                "mchid":"161",
                "mchname":"碑林区运营公司",
                "parkname":"测绘路路北",
                "address":"测绘路路北",
                "citycode":"西安市",
                "areacode":"碑林区",
                "streetcode":"测绘路",
                "lng":108.96202,
                "lat":34.238243,
                "img":null,
                "totalspace":32,
                "usedspace":22,
                "price":2,
                "parktype":1,
                "ischarge":false,
                "isbook":false,
                "isused":true,
                "servicescope":null,
                "text":null,
                "maxPriceOfDay":null,
                "freeMinute":15,
                "dist":1394.5372,
                "unuse":10,
                "isstore":null,
                "paytypelist":null,
                "usableBookingCarportNumber":null,
                "monthvipid":null,
                "pricestrategyid":5,
                "pricenewenergyid":9,
                "innerpricestrategyid":null,
                "strobeflag":null,
                "businesspricestrategyid":null,
                "pricepublicityid":7,
                "pricePublicityFrontList":null,
                "aliParkingid":"",
                "carPortBOS":null,
                "outerinfo":false,
                "limitType":null,
                "limitDate":null,
                "limitTime":null,
                "limitWorkday":null,
                "bookingRule":"",
                "bookingTimeFrameBOS":null,
                "nowtime":null,
                "bookingTimeRule":false,
                "districtid":4,
                "parkArea":2
            },
            {
                "id":96,
                "parkid":"1-01-2-4025",
                "parentid":null,
                "mchid":"161",
                "mchname":"碑林区运营公司",
                "parkname":"皇甫路(兴庆路-交大街段)双侧",
                "address":"皇甫路(兴庆路-交大街段)双侧",
                "citycode":"西安市",
                "areacode":"碑林区",
                "streetcode":"皇甫路",
                "lng":108.99051,
                "lat":34.24799,
                "img":null,
                "totalspace":92,
                "usedspace":31,
                "price":2,
                "parktype":1,
                "ischarge":false,
                "isbook":false,
                "isused":true,
                "servicescope":null,
                "text":null,
                "maxPriceOfDay":null,
                "freeMinute":15,
                "dist":1394.5374,
                "unuse":61,
                "isstore":null,
                "paytypelist":null,
                "usableBookingCarportNumber":null,
                "monthvipid":null,
                "pricestrategyid":5,
                "pricenewenergyid":9,
                "innerpricestrategyid":null,
                "strobeflag":null,
                "businesspricestrategyid":null,
                "pricepublicityid":7,
                "pricePublicityFrontList":null,
                "aliParkingid":"",
                "carPortBOS":null,
                "outerinfo":false,
                "limitType":null,
                "limitDate":null,
                "limitTime":null,
                "limitWorkday":null,
                "bookingRule":"",
                "bookingTimeFrameBOS":null,
                "nowtime":null,
                "bookingTimeRule":false,
                "districtid":5,
                "parkArea":2
            },
            {
                "id":68,
                "parkid":"1-01-2-3031",
                "parentid":null,
                "mchid":"161",
                "mchname":"碑林区运营公司",
                "parkname":"测绘东路(测绘路至友谊东路段)双侧",
                "address":"测绘东路(测绘路至友谊东路段)双侧",
                "citycode":"西安市",
                "areacode":"碑林区",
                "streetcode":"测绘东路",
                "lng":108.960236,
                "lat":34.237778,
                "img":null,
                "totalspace":49,
                "usedspace":32,
                "price":2,
                "parktype":1,
                "ischarge":false,
                "isbook":false,
                "isused":true,
                "servicescope":null,
                "text":null,
                "maxPriceOfDay":null,
                "freeMinute":15,
                "dist":1394.5521,
                "unuse":17,
                "isstore":null,
                "paytypelist":null,
                "usableBookingCarportNumber":null,
                "monthvipid":null,
                "pricestrategyid":5,
                "pricenewenergyid":9,
                "innerpricestrategyid":null,
                "strobeflag":null,
                "businesspricestrategyid":null,
                "pricepublicityid":7,
                "pricePublicityFrontList":null,
                "aliParkingid":"",
                "carPortBOS":null,
                "outerinfo":false,
                "limitType":null,
                "limitDate":null,
                "limitTime":null,
                "limitWorkday":null,
                "bookingRule":"",
                "bookingTimeFrameBOS":null,
                "nowtime":null,
                "bookingTimeRule":false,
                "districtid":4,
                "parkArea":2
            },
            {
                "id":69,
                "parkid":"1-01-2-3032",
                "parentid":null,
                "mchid":"161",
                "mchname":"碑林区运营公司",
                "parkname":"测绘东路(建设路-测绘路段)双侧",
                "address":"测绘东路(建设路-测绘路段)双侧",
                "citycode":"西安市",
                "areacode":"碑林区",
                "streetcode":"测绘东路",
                "lng":108.960236,
                "lat":34.237778,
                "img":null,
                "totalspace":62,
                "usedspace":54,
                "price":2,
                "parktype":1,
                "ischarge":false,
                "isbook":false,
                "isused":true,
                "servicescope":null,
                "text":null,
                "maxPriceOfDay":null,
                "freeMinute":15,
                "dist":1394.5521,
                "unuse":8,
                "isstore":null,
                "paytypelist":null,
                "usableBookingCarportNumber":null,
                "monthvipid":null,
                "pricestrategyid":5,
                "pricenewenergyid":9,
                "innerpricestrategyid":null,
                "strobeflag":null,
                "businesspricestrategyid":null,
                "pricepublicityid":7,
                "pricePublicityFrontList":null,
                "aliParkingid":"",
                "carPortBOS":null,
                "outerinfo":false,
                "limitType":null,
                "limitDate":null,
                "limitTime":null,
                "limitWorkday":null,
                "bookingRule":"",
                "bookingTimeFrameBOS":null,
                "nowtime":null,
                "bookingTimeRule":false,
                "districtid":4,
                "parkArea":2
            },
            {
                "id":61,
                "parkid":"1-01-2-3021",
                "parentid":null,
                "mchid":"161",
                "mchname":"碑林区运营公司",
                "parkname":"草场坡(南二环-永宁路段)双侧",
                "address":"草场坡(南二环-永宁路段)双侧",
                "citycode":"西安市",
                "areacode":"碑林区",
                "streetcode":"草场坡",
                "lng":108.94981,
                "lat":34.23448,
                "img":null,
                "totalspace":142,
                "usedspace":130,
                "price":2,
                "parktype":1,
                "ischarge":false,
                "isbook":false,
                "isused":true,
                "servicescope":null,
                "text":null,
                "maxPriceOfDay":null,
                "freeMinute":15,
                "dist":1394.5819,
                "unuse":12,
                "isstore":null,
                "paytypelist":null,
                "usableBookingCarportNumber":null,
                "monthvipid":null,
                "pricestrategyid":5,
                "pricenewenergyid":9,
                "innerpricestrategyid":null,
                "strobeflag":null,
                "businesspricestrategyid":null,
                "pricepublicityid":7,
                "pricePublicityFrontList":null,
                "aliParkingid":"",
                "carPortBOS":null,
                "outerinfo":false,
                "limitType":null,
                "limitDate":null,
                "limitTime":null,
                "limitWorkday":null,
                "bookingRule":"",
                "bookingTimeFrameBOS":null,
                "nowtime":null,
                "bookingTimeRule":false,
                "districtid":3,
                "parkArea":2
            },
            {
                "id":62,
                "parkid":"1-01-2-3022",
                "parentid":null,
                "mchid":"161",
                "mchname":"碑林区运营公司",
                "parkname":"草场坡(长安北路-永宁路段)双侧",
                "address":"草场坡(长安北路-永宁路段)双侧",
                "citycode":"西安市",
                "areacode":"碑林区",
                "streetcode":"草场坡",
                "lng":108.94981,
                "lat":34.23448,
                "img":null,
                "totalspace":30,
                "usedspace":30,
                "price":2,
                "parktype":1,
                "ischarge":false,
                "isbook":false,
                "isused":true,
                "servicescope":null,
                "text":null,
                "maxPriceOfDay":null,
                "freeMinute":15,
                "dist":1394.5819,
                "unuse":0,
                "isstore":null,
                "paytypelist":null,
                "usableBookingCarportNumber":null,
                "monthvipid":null,
                "pricestrategyid":5,
                "pricenewenergyid":9,
                "innerpricestrategyid":null,
                "strobeflag":null,
                "businesspricestrategyid":null,
                "pricepublicityid":7,
                "pricePublicityFrontList":null,
                "aliParkingid":"",
                "carPortBOS":null,
                "outerinfo":false,
                "limitType":null,
                "limitDate":null,
                "limitTime":null,
                "limitWorkday":null,
                "bookingRule":"",
                "bookingTimeFrameBOS":null,
                "nowtime":null,
                "bookingTimeRule":false,
                "districtid":3,
                "parkArea":2
            },
            {
                "id":137,
                "parkid":"2-01-2-3024",
                "parentid":null,
                "mchid":"161",
                "mchname":"碑林区运营公司",
                "parkname":"友谊东路路南(西安市卫生学校家属院前)",
                "address":"友谊东路路南(西安市卫生学校家属院前)",
                "citycode":"西安市",
                "areacode":"碑林区",
                "streetcode":"友谊东路",
                "lng":108.96803,
                "lat":34.241184,
                "img":null,
                "totalspace":17,
                "usedspace":14,
                "price":2,
                "parktype":1,
                "ischarge":false,
                "isbook":false,
                "isused":true,
                "servicescope":null,
                "text":null,
                "maxPriceOfDay":null,
                "freeMinute":15,
                "dist":1394.6275,
                "unuse":3,
                "isstore":null,
                "paytypelist":null,
                "usableBookingCarportNumber":null,
                "monthvipid":null,
                "pricestrategyid":5,
                "pricenewenergyid":9,
                "innerpricestrategyid":null,
                "strobeflag":null,
                "businesspricestrategyid":null,
                "pricepublicityid":7,
                "pricePublicityFrontList":null,
                "aliParkingid":"",
                "carPortBOS":null,
                "outerinfo":false,
                "limitType":null,
                "limitDate":null,
                "limitTime":null,
                "limitWorkday":null,
                "bookingRule":"",
                "bookingTimeFrameBOS":null,
                "nowtime":null,
                "bookingTimeRule":false,
                "districtid":3,
                "parkArea":2
            },
            {
                "id":139,
                "parkid":"2-01-2-4009",
                "parentid":null,
                "mchid":"161",
                "mchname":"碑林区运营公司",
                "parkname":"友谊东路路南(铁安一街-铁安二街段)",
                "address":"友谊东路路南(铁安一街-铁安二街段)",
                "citycode":"西安市",
                "areacode":"碑林区",
                "streetcode":"友谊东路",
                "lng":108.96803,
                "lat":34.241184,
                "img":null,
                "totalspace":16,
                "usedspace":14,
                "price":2,
                "parktype":1,
                "ischarge":false,
                "isbook":false,
                "isused":true,
                "servicescope":null,
                "text":null,
                "maxPriceOfDay":null,
                "freeMinute":15,
                "dist":1394.6275,
                "unuse":2,
                "isstore":null,
                "paytypelist":null,
                "usableBookingCarportNumber":null,
                "monthvipid":null,
                "pricestrategyid":5,
                "pricenewenergyid":9,
                "innerpricestrategyid":null,
                "strobeflag":null,
                "businesspricestrategyid":null,
                "pricepublicityid":7,
                "pricePublicityFrontList":null,
                "aliParkingid":"",
                "carPortBOS":null,
                "outerinfo":false,
                "limitType":null,
                "limitDate":null,
                "limitTime":null,
                "limitWorkday":null,
                "bookingRule":"",
                "bookingTimeFrameBOS":null,
                "nowtime":null,
                "bookingTimeRule":false,
                "districtid":4,
                "parkArea":2
            },
          
        ],
        "searchCount":true
    }
}

问题报错怎么解决

  • 写回答

4条回答 默认 最新

  • P2441M 2023-06-05 10:11
    关注
    import json
    import csv
    
    rows = []
    
    # 不用二进制读取
    with open('4_.json', 'r', encoding='utf-8') as f:
        data_list = json.load(f)
        arr = data_list['data']['list']
        print(arr)
        for data in arr:
            # 提取停车场信息
            mchname = data['mchname']
            parkname = data['parkname']
            totalspace = data['totalspace']
            usedspace = data['usedspace']
            freeMinute = data['freeMinute']
            rows.append([mchname, parkname, totalspace, usedspace, freeMinute])
    
    with open('parking_info2.csv', 'w', newline='', encoding='utf-8') as f:
        writer = csv.writer(f)
        writer.writerow(['运营公司', '停车场名称', '总车位数', '当前使用车位数', '免费时长'])
        writer.writerows(rows)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 6月13日
  • 已采纳回答 6月5日
  • 创建了问题 6月5日

悬赏问题

  • ¥15 verilog 非阻塞赋值下的移位拼接错误
  • ¥100 两个按钮控制一个LED
  • ¥15 用C语言写离散数学相关问题
  • ¥30 如何用python的GephiStreamer连接到gephi中,把Python和Gephi的具体操作过程都展示,重点回答Gephi软件的调试,以及如果代码的端口在浏览器中无法显示怎么处理
  • ¥15 ansys机翼建模肋参数
  • ¥15 Sumo软件无法运行
  • ¥15 如何在vscode里搭建stata的编辑环境?
  • ¥15 dify知识库创建问题
  • ¥15 如何用C#的chart画1000万个点不卡顿
  • ¥15 爬虫技术找到网上看过房源客户的电话