Json转excel

Json转excel

1.准备环境
pip install pandas

准备数据

下文为 name.json内容:

{"time": "20220808", "name": "why", "age": "20"}
{"time": "20220809", "name": "duet", "age": "18"}
{"time": "20220810", "name": "wxm", "age": "18"}
{"time": "20220811", "name": "dsb", "age": "19"}

以下为python 代码:

import json
import pandas as pd

df = pd.read_json(r'/root/duet/python/json/name.json', encoding ='UTF-8', lines =True)
df.to_excel(r'/root/duet/python/excel/name.xlsx', index=False)

2.内嵌的 JSON 数据文件
以下为内嵌json 文件name1.json

{
    "RequestId": "9602330-D5B7-5616-A077-121212F",
    "class": "class 1",
    "Data": {
        "students": [
            {
                "id": "001",
                "name": "yhy",
                "math": 20,
                "physics": 10,
                "chemistry": 30
            },
            {
                "id": "002",
                "name": "duet",
                "math": 50,
                "physics": 77,
                "chemistry": 87
            },
            {
                "id": "003",
                "name": "ww",
                "math": 100,
                "physics": 90,
                "chemistry": 88
            }]
    }
}

以下为python代码

import pandas as pd
import json

with open(r'/root/duet/python/json/name1.json',encoding='UTF-8') as f:
    data = json.loads(f.read())
df = pd.json_normalize(
    data,
    record_path =['Data','students']
)
df.to_excel("output.xlsx")

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值