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= ...
随机推荐
- Linux 串口、usb转串口驱动分析(2-2) 【转】
转自:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=26807463&id=4186852 Linux 串口.usb转 ...
- MySQL5.7 GTID在线开启与关闭【转】
当前场景 当前某些业务还有未开启GTID服务组,升级5.7后,如何检测是否符合开启GTID条件,如何在线修改切换使用GTID:已经升级5.7后,已经开启GTID,如何快速回滚后退: 线上gtid如 ...
- retrying模块的学习
retrying模块的学习 我们在写爬虫的过程中,经常遇到爬取失败的情况,这个时候我们一般会通过try块去进行重试,但是每次都写那么一堆try块,真的是太麻烦,所以今天就来说一个比较pythonic的 ...
- WCF服务安全控制之netTcpBinding的用户名密码验证【转】
选择netTcpBinding WCF的绑定方式比较多,常用的大体有四种: wsHttpBinding basicHttpBinding netTcpBinding wsDualHttpBinding ...
- Java中抽象类概述
抽象类 1.抽象类的定义 抽象类是为子类提供一个规范,这就必须联系到继承: 抽象类的制定就是让子类继承的: public abstract 类名{ //类体 //抽象方法 修饰符 abstract ...
- C# Message类的属性Msg所关联的消息ID
C# Message类的属性Msg所关联的消息ID https://msdn.microsoft.com/en-us/library/windows/desktop/ms645606(v=vs.8 ...
- px、pt、ppi、dpi、dp、sp之间的关系
http://www.woshipm.com/pmd/176328.html 各自的定义: px:pixel,像素,电子屏幕上组成一幅图画或照片的最基本单元 pt: point,点,印刷行业常用单位, ...
- rabbitmq route
AMQP AMQP协议是一个高级抽象层消息通信协议,RabbitMQ是AMQP协议的实现.它主要包括以下组件: 1.Server(broker): 接受客户端连接,实现AMQP消息队列和路由功能的进程 ...
- Java第三阶段学习(九、类加载器、反射)
一.类加载器 1.类的加载: 当程序要使用某个类时,如果该类还未被加载到内存中,则系统会通过加载,连接,初始化三步来实现对这个类进行初始化. 1.1 加载: 就是指将class文件读入内存,并为之自动 ...
- API的防重
说说API的防重放机制 2017-03-20 18:19 by 轩脉刃, 685 阅读, 7 评论, 收藏, 编辑 说说API的防重放机制 我们在设计接口的时候,最怕一个接口被用户截取用于重放攻击.重 ...