代码
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
}
}
问题报错怎么解决
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)
该回答引用chatgpt:
有两个错误
1.json文件格式不正确,多了一个逗号
2.读取json也不对
下面是修正后的代码
import json
import csv
# 打开JSON文件并加载数据
with open('4_.json', 'r') as f:
data = json.load(f)
arr = data['data']['list']
# 将信息写入CSV文件
with open('parking_info2.csv', 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow(['运营公司', '停车场名称', '总车位数', '当前使用车位数', '免费时长'])
for item in arr:
mchname = item['mchname']
parkname = item['parkname']
totalspace = item['totalspace']
usedspace = item['usedspace']
freeMinute = item['freeMinute']
writer.writerow([mchname, parkname, totalspace, usedspace, freeMinute])
{
"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
}
}
取list的时候稍微有点问题,改成这样就好了:
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['list']:
# 提取停车场信息
mchname = data['mchname']
parkname = data['parkname']
totalspace = data['totalspace']
usedspace = data['usedspace']
freeMinute = data['freeMinute']
# 将信息写入CSV文件
with open('parking_info2.csv', 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow(['运营公司', '停车场名称', '总车位数', '当前使用车位数','免费时长'])
另外,你的json文件424行那个逗号可以去掉,有报错。
试下,应该解决了你的问题。
要是还不行,你喊我再来解决
首先导入两个包:
import json
import pandas as pd
打开json 文件并读取:
with open('2.json', encoding='utf-8') as f:
line = f.readline()
d = json.loads(line)
f.close()
读取的json数据会以字典的形势保存,按照字典的读取方式获取自己想要的数据:
datas_x = []
datas_y = []
for dss in d:
datas_x.append(float(dss["pos"]["x"]))
datas_y.append(float(dss["pos"]["z"]))
将数据保存到列表中,然后创建pandas的DataFrame,DataFrame是由多种类型的列构成的二维标签数据结构。
path_x = pd.Series(datas_x)
path_y = pd.Series(datas_y)
path_df = pd.DataFrame()
path_df['pathx'] = path_x
path_df['pathy'] = path_y
最后将数据保存到csv中。
filepath = "E:\\python\\python\\2021\\202104\\0409\\path_data.csv"
path_df.to_csv(filepath, index=False, header=False)
完整代码:
import json
import pandas as pd
filepath = "E:\\python\\python\\2021\\202104\\0409\\path_data.csv"
with open('2.json', encoding='utf-8') as f:
line = f.readline()
d = json.loads(line)
f.close()
datas_x = []
datas_y = []
for dss in d:
datas_x.append(float(dss["pos"]["x"]))
datas_y.append(float(dss["pos"]["z"]))
path_x = pd.Series(datas_x)
path_y = pd.Series(datas_y)
path_df = pd.DataFrame()
path_df['pathx'] = path_x
path_df['pathy'] = path_y
path_df.to_csv(filepath, index=False, header=False)