Python3 -- 通过pandas实现快速创建java实体代码模板

有过面向对象开发经验的同学,肯定遇到过,创建一个Bean对象其中有很多属性,特别是在对接第三方接口的时候,要将第三方的输入输出参数,封装成对象,一个一个复制粘贴,是不是很低效。

下面我通过python实现了一个自动生成代码简易模板,

使用前,需要将我们生成代码的属性,和属性含义,写入到excel中,如下图
这里写图片描述

图中几个红框的需要注意

  • excel名称必须叫:BEAN.xls
  • 第一行有2列,第一列叫:name,第二列是remark,这么叫是我了python中根据这个名字获取数据。
  • 必须是sheet1页,程序中写死的

看python代码:

# Import pandas
import pandas as pd

# Assign spreadsheet filename to `file`
file = 'C:/Users/Administrator/PycharmProjects/Demo/excel/BEAN.xls'

# Load spreadsheet
xl = pd.ExcelFile(file)

# Print the sheet names
# print(xl.sheet_names)

# Load a sheet into a DataFrame by name: df1
df1 = xl.parse('Sheet1')

names = df1['name']
remarks = df1['remark']

i = 0
for name in names:
    print('/**\n* ' + remarks[i] + '\n*/')
    print('private String ' + name + ' = "";')
    i = i + 1

运行结果:

C:\Users\Administrator\PycharmProjects\Demo\venv\Scripts\python.exe C:/Users/Administrator/PycharmProjects/Demo/excel/genBean.py
/**
* 商户编号 
*/
private String merchantno  = "";
/**
* 绑卡请求号 
*/
private String requestno  = "";
/**
* 易宝流水号 
*/
private String yborderid  = "";
/**
* 订单状态 
*/
private String status  = "";
/**
* 短验码 
*/
private String smscode  = "";
/**
* 实际短验发送方 
*/
private String codesender  = "";
/**
* 支持的补充鉴权类型 
*/
private String enhancedtype  = "";
/**
* 实际短验发送类型 
*/
private String smstype  = "";
/**
* 错误码 
*/
private String errorcode  = "";
/**
* 错误信息 
*/
private String errormsg  = "";
/**
* 签名 
*/
private String sign  = "";

Process finished with exit code 0

代码很简单,因为方便自己开发使用,没有做的太繁琐,简单易懂实现功能即可,如果有更多需求,可以继续扩展。

猜你喜欢

转载自blog.csdn.net/u010926176/article/details/79354289