pandas读取excel封装成json数据格式的工作中的应用

本文介绍了如何用pandas解析Excel,将其转化为JSON格式,便于生成C/C++头文件,简化了代码并提高效率。
摘要由CSDN通过智能技术生成

上篇博文《用python的mako模板库写一个自动生成.c/.h文件generate_code功能》中,其中解析excel这块儿,我用的是xlwings这个包,其中还有没解决的问题。这篇文章换用pandas解析excel并封装为json格式的。

要处理的数据是这样的:

名称变量代码信号名称起始位信号长度分辨率偏移量初始值无效值信号值描述变量代码类型
idvar_codesignal_namestart_bitsignal_lenresolutionoffsetinitial_valinvalid_valsignal_commentvar_type
0x387TBOX_Satellite_Time_Hour小时248100x0/0x0:0int8_t
0x387TBOX_SatelliteTime_Minute分钟328100x0/0x17:23 0x0:0 0x3B:59int8_t
0x999TBOX_SatelliteTime_Minute分钟328100x0/0x17:23 0x0:0 0x3B:59int8_t

 处理后我想要的数据json格式是这样的:

[
    {
      "id": "0x387",
      "values": [
        {
          "id": "0x387",
          "var_code": "TBOX_Satellite_Time_Hour",
          "signal_name": "小时",
          "start_bit": 24,
          "signal_len": 8,
          "resolution": 1,
          "offset": 0,
          "initial_val": "0x0",
          "invalid_val": "\/",
          "signal_comment": "0x0:0",
          "var_type": "int8_t"
        },
        {
          "id": "0x387",
          "var_code": "TBOX_SatelliteTime_Minute",
          "signal_name": "分钟",
          "start_bit": 32,
          "signal_len": 8,
          "resolution": 1,
          "offset": 0,
          "initial_val": "0x0",
          "invalid_val": "\/",
          "signal_comment": "0x17:23 0x0:0 0x3B:59",
          "var_type": "int8_t"
        }
      ]
    },
    {
      "id": "0x999",
      "values": [
        {
          "id": "0x999",
          "var_code": "TBOX_SatelliteTime_Minute",
          "signal_name": "分钟",
          "start_bit": 32,
          "signal_len": 8,
          "resolution": 1,
          "offset": 0,
          "initial_val": "0x0",
          "invalid_val": "\/",
          "signal_comment": "0x17:23 0x0:0 0x3B:59",
          "var_type": "int8_t"
        }
      ]
    }
  ]

用pandas库这样写,满足了我的要求:

import os

import pandas as pd
from mako.template import Template
from pandas.io import json

''' 1,解析excel,提取 entity 数据信息封装成dic'''


def parse_excel(file_path):
    df = pd.read_excel(file_path, header=1)
    res = df.groupby('id').apply(lambda x: x.to_dict('records'))
    # res.to_json('data.json',orient='table',force_ascii=False)
    return res.to_json(orient='table', force_ascii=False)

''' 2.根据mako模板生成.h/.c文件'''

def generate_code_h(tables: json):
    h_file = Template(filename='./mako_template/entity_h.mako', module_directory='./mako_template/tmp').render(
        **json.loads(tables))
    out_file = './generate_file/'
    if not os.path.exists(out_file):
        os.makedirs(out_file)
    with open(out_file + 'entity_h.h', 'wb+') as f:
        f.write(h_file.encode('utf-8'))
        print('h_file 文件生成,成功 @——@')
        return os.path.abspath(out_file + 'entity_h.h')


if __name__ == '__main__':
    excel = 'C:\\Users\\15839680585\\Desktop\\template\\Desktop\\entity2.xlsx'
    generate_code_h(parse_excel(excel))

从结果上可以看出,这样写的话,对比上篇,代码会少很多,总重要的是不用自己去思考表格的处理细节。

掌握潘达 提供的一些excel的处理方法,对日常工作中表格数据更方便。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值