用python实现多嵌套json数据存入excel表格——以美食数据为例

json嵌套格式如下:

主要为
[区位[商家{菜单}],区位[商家{菜单}],…,区位[商家{菜单}]}的数据格式

{
	"id": 13779,
	"code": "xa",
	"pinyin": "xian",
	"addr": "西安",
	"areaId": 113,
	"areaName": "碑林区",
	"level": 2,
	"parentId": 2131,
	"eshopList": [
		{
			"id": 2106,
			"addrId": 5897,
			"shopId": "E9347828247610564279",
			"name": "极速披萨外卖",
			"brand": null,
			"recentOrderNum": 1335,
			"imagePath": "078a1ff5a967e578bee6614d93adf9fajpeg",
			"rating": "4.7",
			"ratingCount": "376",
			"address": "西安市碑林区东关南街29号2号楼101室",
			"phone": "13759967293 13619256197",
			"latitude": "34.25375",
			"longitude": "108.97534",
			"description": null,
			"areaName": "碑林区",
			"city": null,
			"limitNum": null,
			"menuList": [省略,其中包含多个字典]
			"eaddr": null
		},
		{
			"id": 5450,
			"addrId": 5906,
			"shopId": "E8089558816195246680",
			"name": "麻辣诱惑小龙虾(南稍门店)",
			"brand": null,
			"recentOrderNum": 893,
			"imagePath": "7b257dc4ca9f48179834f09de8400c0djpeg",
			"rating": "4.6",
			"ratingCount": "265",
			"address": "西安市碑林区南关正街四民巷55号8号门面",
			"phone": "18092444501 029-68889905",
			"latitude": "34.242153",
			"longitude": "108.945946",
			"description": null,
			"areaName": "碑林区",
			"city": null,
			"limitNum": null,
			"menuList": [],
			"eaddr": null
		},.............省略若干商家
 },
{
	"id": 13780,
	"code": "xa",
	"pinyin": "xian",
	"addr": "西安",
	"areaId": 116,
	"areaName": "雁塔区",
	"level": 2,
	"parentId": 2131,
	"eshopList": [
	{
	},
	.....
	.....

python代码,这里实现的是存入各个区前30家店铺信息:

import json,xlwt

# 解析json
def readJsonfile():
    jsobj=json.load(open(r'F:\毕业论文\统计分析.json'))
    return jsobj
#转换为excel
def Json2Excel():
    jsonfile=readJsonfile()
    workbook=xlwt.Workbook()
    sheet1=workbook.add_sheet('各区店铺(前30)')
    start_Row=start_Column=0
    # 第一层循环,依次取出区位字典,t代表区位号
    for t in range(0,len(jsonfile)):
        regiondict=jsonfile[t]
        region=regiondict["areaName"]
        sheet1.write(start_Row,start_Column,region)
        shopList=regiondict["eshopList"]
        # 第二层循环,依次取出商家列表,i代表商家号
        for i in range(0,len(shopList)):
            one_shop=shopList[i]
            # 构建空列表存放商字典中的value
            one_shop_list=[]
            for x in one_shop.values():
                one_shop_list.append(x)
            # 第三层循环,依次按列写入商家信息
            for j in range(1,13):
                next_Row=start_Row+i
                sheet1.write(next_Row,j,one_shop_list[j])   
            workbook.save('西安美食数据.xls')
        # 避免商家不足30的状况
        if i !=29:
            next_Row=start_Row+29
        start_Row=next_Row+1
        j=1
Json2Excel()

同理,存入菜单信息也是一步一步循环再按行列写入即可~~

  • 8
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值