python脚本从excel表到处数据,生成指定格式的文件
#coding:gbk
#导入处理excel的模块
import xlrd #定义哪些字段须要推断,仅仅支持时间字段
toSureColArray = ['CREATE_TIME','MODIFY_TIME'] #确定某个字段是否在数组中
def isColInArray(colName, colArray):
for i in range(0, len(colArray)):
if (colName == colArray[i]):
return 1
else:
return 0 #定义子sheet字段属性映射处理函数
#入參为文件描写叙述符、、源表列字段、和目表列字段
def childSheetColFunc(fOutput, sourceTabCol, destTabCol,destTableSureArray):
for i in range(2,len(sourceTabCol)):
if len(destTabCol[i]) != 0:
if len(sourceTabCol[i]) != 0:
#在子表中,假设浪潮int_id相应的直真字段英文名称为空,在destTableSureArry不存在INT_ID属性
destTableSureArray.append(str(destTabCol[i]).upper())
fOutput.write("\t\t\t\t\tA."+str(sourceTabCol[i]).upper()+" "+str(destTabCol[i]).upper()+",\n")
else:
continue
else:
continue
#for i in range(0,len(destTableSureArray)):
# print("******************")
# print(str(destTableSureArray[i]))
# print("******************") #对creat_time和modify_time做单独处理
for i in range(0,len(toSureColArray)):
if isColInArray(toSureColArray[i],destTableSureArray) == 0:
fOutput.write("\t\t\t\t\tSYSDATE "+toSureColArray[i]+",\n")
destTableSureArray.append(toSureColArray[i]) #定义子sheet目标数据插入函数主键模块
#入參为文件描写叙述符、源表列字段、目表源字段
def childSheetColDesKeyFunc(fOutput, sourceTabCol, destTabCol):
#假设为空表,此处会报下标溢出错误,所以入參必须保证表中有数据
if len(sourceTabCol[2]) != 0:
fOutput.write("\t\t\t\t<col name=\""+str(destTabCol[2]).upper()+"\">["+str(destTabCol[2]).upper()+"]</col>\n")
fOutput.write("\t\t\t\t<col name=\"STATEFLAG\">[STATEFLAG]</col>\n") #定义子sheet目标函数插入函数非主键模块
def childSheetColDesNKeyFunc(fOutput, sourceTabCol, destTableSureArray):
if len(sourceTabCol[2]) != 0:
for i in range(1,len(destTableSureArray)):
fOutput.write("\t\t\t\t<col name=\""+destTableSureArray[i]+"\">["+destTableSureArray[i]+"]</col>\n")
else:
for i in range(0,len(destTableSureArray)):
fOutput.write("\t\t\t\t<col name=\""+destTableSureArray[i]+"\">["+destTableSureArray[i]+"]</col>\n") #定义子sheet处理函数
def childSheetFunc(sheetName, sourceTable, destTable):
#依据sheet名打开表
shtInstance=workBook.sheet_by_name(sheetName)
#存放终于哪些浪潮字段须要映射
destTableSureArray = [] langchaoTabCol=shtInstance.col_values(4)
zhizhenTabCol=shtInstance.col_values(6)
print("***********如今開始生成"+str(destTable).upper()+".xml 文件***********")
path="e:\\"+destTable.upper()+".xml"
fOutput=open(path,"w")
fOutput.write("<? xml version=\"1.0\" encoding=\"gb2312\"? >\n")
fOutput.write("<Conf>\n")
fOutput.write("\t<stage id=\"1\" describe=\""+str(sourceTable).upper()+"\" doUpdate=\"$doUpdate$\">\n")
fOutput.write("\t\t<drawout DBid=\"ZZ\">\n")
fOutput.write("\t\t\t<![CDATA[\n")
fOutput.write("\t\t\t\tSELECT\n")
fOutput.write("\t\t\t\t\t0 STATEFLAG,\n")
childSheetColFunc(fOutput, zhizhenTabCol, langchaoTabCol,destTableSureArray)
#fOutput.write("\t\t\t\t\tSYSDATE STATEFLAG\n")
fOutput.write("\t\t\t\tFROM INFORMIX."+str(sourceTable).upper()+" A\n")
fOutput.write("\t\t\t\tWHERE 1=1\n")
fOutput.write("\t\t\t\t\tAND ROW_NUM < 2\n")
fOutput.write("\t\t\t]]>\n")
fOutput.write("\t\t</drawout>\n")
fOutput.write("\t\t<Load DBid=\"IRMS_143\" tableName=\""+str(destTable).upper()+"\"\n")
fOutput.write("\t\t\t<Dimension>\n")
childSheetColDesKeyFunc(fOutput, zhizhenTabCol, langchaoTabCol)
fOutput.write("\t\t\t<Dimension>\n")
fOutput.write("\t\t\t<Cols>\n")
childSheetColDesNKeyFunc(fOutput, zhizhenTabCol, destTableSureArray)
fOutput.write("\t\t\t<Cols>\n")
fOutput.write("\t\t</Load>\n")
fOutput.write("\t</stage>\n")
fOutput.write("</Conf>\n")
fOutput.close()
print("***********"+str(destTable).upper()+".xml 文件生成完毕***********") #依据主表获取子表信息并处理
def mainSheetAndProc(mainSheet): sheetName=mainSheet.col_values(2)
sourceTabName=mainSheet.col_values(5)
destTabName=mainSheet.col_values(3)
curNum = 0
for i in range(2,len(sheetName)):
if len(sheetName[i]) ==0:
continue
curNum += 1
print(">>>>>>開始生成第"+str(curNum)+"个文件")
childSheetFunc(sheetName[i], sourceTabName[i], destTabName[i]) #入口函数
if __name__=="__main__":
workBook=xlrd.open_workbook("E:\\内线比对模型.xls")
mainSheet=workBook.sheets()[0]
mainSheetAndProc(mainSheet)
python脚本从excel表到处数据,生成指定格式的文件的更多相关文章
- Delphi中使用python脚本读取Excel数据
Delphi中使用python脚本读取Excel数据2007-10-18 17:28:22标签:Delphi Excel python原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 . ...
- python xlrd 模块(获取Excel表中数据)
python xlrd 模块(获取Excel表中数据) 一.安装xlrd模块 到python官网下载http://pypi.python.org/pypi/xlrd模块安装,前提是已经安装了pyt ...
- 用CBrother将excel中的数据转换为C语言头文件
用CBrother将excel中的数据转换为C语言头文件 最近在工作中,产品这边总是要调整一些参数,而我们在这一块要求所有的配置必须用宏定义来做,因为不同型号直接硬编码写死在代码里,但是一但数量大了, ...
- 利用caffe生成 lmdb 格式的文件,并对网络进行FineTuning
利用caffe生成 lmdb 格式的文件,并对网络进行FineTuning 数据的组织格式为: 首先,所需要的脚本指令路径为: /home/wangxiao/Downloads/caffe-maste ...
- 关于asp.net C# 导出Excel文件 打开Excel文件格式与扩展名指定格式不一致的解决办法
-----转载:http://blog.csdn.net/sgear/article/details/7663502 关于asp.net C# 导出Excel文件 打开Excel文件格式与扩展名指定格 ...
- linux生成指定大小的文件(转)
# dd if=/dev/zero of=50M.file bs=1M count=50在当前目录下生成一个50M的文件 虚拟块设备文件更通用的名称是硬盘镜像文件(Hard Disk Image),但 ...
- vbs 之 解决打开Excel文件格式与扩展名指定格式不一致的问题
' Q:解决打开Excel文件格式与扩展名指定格式不一致的问题' A: 使用工作簿saveAs时,往往忽略掉它的第二个参数FileFormat,添加即可. 比如: set bookDiff = oEx ...
- julia生成指定格式的字符串.jl
julia生成指定格式的字符串.jl """ julia生成指定格式的字符串.jl http://bbs.bathome.net/thread-39829-1-1.htm ...
- Python读取excel表的数据
from openpyxl.reader.excel import load_workbook #读取xlsx文件def readExcelFile(path): dic={} #打开文件 file= ...
随机推荐
- 【逆向工具】IDA Python安装与使用
1.IDA Pyhon介绍 IDA Python是IDA6.8后自带插件,可以使用Python做很多的辅助操作,非常方便的感觉. 2.IDA Python安装 从github上IDAPython项目获 ...
- oracle数据库_实例_用户_表空间之间的关系
基础概念:Oracle数据库.实例.用户.表空间.表之间的关系 数据库:Oracle数据库是数据的物理存储.这就包括(数据文件ORA或者DBF.控制文件.联机日志.参数文件).其实Oracle数据库的 ...
- 使用 Application Loader提交IPA文件到苹果市场
打包.导出ipa包后剩下的就是要将ipa包推到appstore.Application Loader是苹果提供的ipa包提交工具. 1.启动Application Loader 打开xcode,在xc ...
- window10 显示QQ图标
- 在idea中关闭vim模式
每次在idea的文件中插入新的内容时,都需要先点击键盘上的i 进入插入模式,感觉这是vim编辑器的模式,很不习惯. 你可能是按照了vim emulation 插件, 在setting-----plug ...
- CSS工具之CSS重置(CSS Reset)
css代码: /* v1.0 | 20080212 */ html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, ...
- HTML5 标签语法变化和使用概念
1.H5与H4的区别 概念的变化: H5更注重内容与结构,不再只专注于表现. 声明与标签: 新的声明背简化: <!DOCTYPE html> <meta charset=utf-8& ...
- [C编码笔记] 空串与NULL是不一样的
int main() { char *str = NULL; printf("%p \n", str); printf("%p \n", &str); ...
- 001.KVM介绍
KVM介绍可参考: http://liqingbiao.blog.51cto.com/3044896/1740516 http://koumm.blog.51cto.com/703525/128879 ...
- SDC信息统计分析系统ETL工具的研究与实现[专业:计算机应用技术]
SDC信息统计分析系统ETL工具的研究与实现[专业:计算机应用技术] http://www.docin.com/p-265530271.html