#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表到处数据,生成指定格式的文件的更多相关文章

  1. Delphi中使用python脚本读取Excel数据

    Delphi中使用python脚本读取Excel数据2007-10-18 17:28:22标签:Delphi Excel python原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 . ...

  2. python xlrd 模块(获取Excel表中数据)

    python xlrd 模块(获取Excel表中数据) 一.安装xlrd模块   到python官网下载http://pypi.python.org/pypi/xlrd模块安装,前提是已经安装了pyt ...

  3. 用CBrother将excel中的数据转换为C语言头文件

    用CBrother将excel中的数据转换为C语言头文件 最近在工作中,产品这边总是要调整一些参数,而我们在这一块要求所有的配置必须用宏定义来做,因为不同型号直接硬编码写死在代码里,但是一但数量大了, ...

  4. 利用caffe生成 lmdb 格式的文件,并对网络进行FineTuning

    利用caffe生成 lmdb 格式的文件,并对网络进行FineTuning 数据的组织格式为: 首先,所需要的脚本指令路径为: /home/wangxiao/Downloads/caffe-maste ...

  5. 关于asp.net C# 导出Excel文件 打开Excel文件格式与扩展名指定格式不一致的解决办法

    -----转载:http://blog.csdn.net/sgear/article/details/7663502 关于asp.net C# 导出Excel文件 打开Excel文件格式与扩展名指定格 ...

  6. linux生成指定大小的文件(转)

    # dd if=/dev/zero of=50M.file bs=1M count=50在当前目录下生成一个50M的文件 虚拟块设备文件更通用的名称是硬盘镜像文件(Hard Disk Image),但 ...

  7. vbs 之 解决打开Excel文件格式与扩展名指定格式不一致的问题

    ' Q:解决打开Excel文件格式与扩展名指定格式不一致的问题' A: 使用工作簿saveAs时,往往忽略掉它的第二个参数FileFormat,添加即可. 比如: set bookDiff = oEx ...

  8. julia生成指定格式的字符串.jl

    julia生成指定格式的字符串.jl """ julia生成指定格式的字符串.jl http://bbs.bathome.net/thread-39829-1-1.htm ...

  9. Python读取excel表的数据

    from openpyxl.reader.excel import load_workbook #读取xlsx文件def readExcelFile(path): dic={} #打开文件 file= ...

随机推荐

  1. 规则 防火墙 iptables input accept【转】

    由于 mangle 这个表格很少被使用,如果将图 9.3-3 的 mangle 拿掉的话,那就容易看的多了: 图 9.3-4.iptables 内建各表格与链的相关性(简图) 透过图 9.3-4 你就 ...

  2. 促使团队紧密协作[高效能程序员的修炼-N1]

    在Jeff看来,团队里最重要的事情,是人与人之间地协作和沟通!所有的问题,其实都是人的问题.“不管什么问题,那总是人的问题”-温伯格.即,让你和团队陷入困境的最快的方法,就是认为技术是决定性的因素,而 ...

  3. 数据库索引和SQL语句使用经验

    1.如果检索数据量超过30%的表中记录数,使用索引将没有显著的效率提高 2.在特定情况下,使用索引也许会比全表扫描慢,但这是同一个数量级上的差距:而通常情况下,使用索引比全表扫描要快几倍乃至几千倍! ...

  4. [WC2008]游览计划 「斯坦那树模板」

    斯坦那树 百度释义 斯坦纳树问题是组合优化问题,与最小生成树相似,是最短网络的一种.最小生成树是在给定的点集和边中寻求最短网络使所有点连通.而最小斯坦纳树允许在给定点外增加额外的点,使生成的最短网络开 ...

  5. Kali Linux上安装SSH服务

    安装 SSH 从终端使用 apt-get 命令安装 SSH 包: # apt-get update # apt-get install ssh 启用和开始使用 SSH 为了确保安全 shell 能够使 ...

  6. SpringBoot修改默认端口号,session超时时间

    有时候我们可能需要启动不止一个SpringBoot,而SpringBoot默认的端口号是8080,所以这时候我们就需要修改SpringBoot的默认端口了.修改SpringBoot的默认端口有两种方式 ...

  7. Mac 命令行安装 dmg文件

    1.安装dmg文件 hdiutil attach jdk-9.0.1_osx-x64_bin.dmg 会挂载在 /Volumes 目录下 2.安装pkg文件(可以 man installer 查看命令 ...

  8. BI生态圈常用端口使用配置总结

    Hadoop集群的各部分一般都会使用到多个端口,有些是daemon之间进行交互之用,有些是用于RPC访问以及HTTP访问.而随着Hadoop周边组件的增多,完全记不住哪个端口对应哪个应用,特收集记录如 ...

  9. c++ primer 学习杂记1

    读到p483 公有,私有,受保护的继承. 1.关于基类成员在派生类中的访问级别: 1) 无论何种继承方式, 派生类都无法访问基类中的private成员. 2) 派生类可以限制,而不能放松对所继承成员的 ...

  10. JS实现集合和ECMA6集合

    集合类似于数组,但是集合中的元素是唯一的,没有重复值的.就像你学高中数学的概念一样,集合还可以做很多比如,并集,交集,差集的计算.在ECMA6之前,JavaScript没有提供原生的Set类,所以只能 ...