版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_28847617/article/details/85038074
(1)、主调程序: main.py
#########################入口主程序########################## import json_text try: json_str=input("请输入json文件的绝对路径名称:") json_text.json_file(json_str) except Exception as error_info: print("异常信息",error_info) finally: exit()
(2)、校验JSON文件的模块:
json_text.py
import json import re import write_data ########################################################### # # # ------- 分析函数方法的功能:传递json文件 ---------- # # # ############################################################ def json_file(json_name): file_name=json_name ############接下来校验json文件名是否合格################## if re.search(".json",file_name,re.I):##########校验JSON文件的格式后缀 try: with open(file_name,'rb') as json_str: data_dir=json.load(json_str) ###########把json文件的内容解码成一种python格式{属性:值} ########把表头字符串解析出来,转换成列表类型############## head_text=re.split(',',data_dir['head_name']) print(head_text) except Exception as error_info: print(error_info) else: ''' 正常解析json,可以执行调用方法来,生成xlsx表格数据 ''' if data_dir['sql_text']!="" and data_dir['file_name']!="" and head_text!="": write_data.check_paramter_listinfo(data_dir['sql_text'],data_dir['file_name'],head_text) else: print("json文件格式不对") else: print("校验JSON文件失败")
(3)、生成xlsx表格数据的程序:
write_data.py
from NOP_db import NOP_db #####导入数据库的参数信息 import cx_Oracle as oracle_11g #######导入oracle数据库驱动模块 import xlwt ######################################################################################### # --------------- 分 析 函 数 out_write()的功能 ------------------- # # ---------------- 通过参数来判断以下的第一个功能是生成xlsx表格文件数据 --------------- # # 第一个参数是:select_sql 第二个参数是:文件名称(可绝对路径) 第三个参数是创建表头 # ######################################################################################### def out_write_xlsx(head_name,sql,file_name): ref_object = NOP_db(1) ref_condb = ref_object.conn_db() ###############创建一个xlsx表格文件######### ref_xls = xlwt.Workbook() sheet_name = ref_xls.add_sheet("sheet1") head_sum=len(head_name) head_start=0 ########################写表头信息########################## while head_start<head_sum: sheet_name.write(0,head_start,head_name[head_start]) head_start=head_start+1 if ref_condb: print(ref_condb) #####如果是连接数据库失败,则返回Fasle, 否则返回一个成功生成对象的一个唯一标识(<cx_Oracle.Connection to YN_MH@nopdb1>) #################执行传递动态selectSQL来完成查询############## ########### 可以根据不同文件来完成不同的动态SQL查询 ############## select_sql=sql rows_list=ref_object.select_row(ref_condb,select_sql) ###print(rows_list) 获取的返回值是:[(第一行),(第二行)] ----每行都是一个元组类型(字段1,字段2,字段3) row_sum=len(rows_list) row_index=0 #########用来控制循环写,写到有多少返回值就写多少行 head_line=1 #########应该为表头,所以行号从1开始 while row_index<row_sum: colum_sum=rows_list[row_index] ########每行有多少列 colum=0 ############如果需要表头的话,从xlsx表格的第二行开始写。 for danyaunge in rows_list[row_index]: ########开始读取每一行的各个字段属性值 sheet_name.write(head_line,colum,danyaunge) colum=colum+1##############每读取完一列数据就往上加一个值 row_index=row_index+1#########每写完一行数据,索引值就+1,直到加到最大索引值为止 head_line=head_line+1 ref_xls.save(file_name) ref_condb.close() else: print("数据库连接失败,未获取到会话") ######################################################################################## # --------------- 分 析函数check_paramter_listinfo()的功能 ------------------ # # ----------- 核查参数数据是否都不为空 ------------------ # ######################################################################################### def check_paramter_listinfo(sql_text,file_name,head_str): if sql_text!="" and file_name!="" and head_str!="": print("满足条件可以执行程序生成xlsx表格数据") out_write_xlsx(head_str,sql_text,file_name) else: print("传递的参数信息有问题")
(4)、JSON文件数据:
data_get.json
{
"sql_text":"select username,passwd,age from user_msg",
"file_name":"C:/Users/10468/Desktop/数据提取.xlsx",
"head_name":"username,passwd,age"
}
(5)、执行输出效果: