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= ...
随机推荐
- cefSharp获取百度搜索结果页面的源码
using CefSharp; using CefSharp.WinForms; using System; using System.Collections.Generic; using Syste ...
- 阿里云RDS的mysql数据库占用空间超过90%的处理
阿里云RDS数据库最大支持2T,目前已经占用了90%,如果进行分库或者迁移比较麻烦,思路是找出占用空间过大的日志或不重要的文件进行删除操作 查询所有数据库占用磁盘空间大小的SQL语句: show bi ...
- jenkins的svn路径中文问题
今天弄Jenkins,我们的SVN代码路径是中文的,他娘的坑死我了,很没面子弄了俩点,网上方案试了好多,说装插件,修改Tomcat server.xml,基本没用,后来看到一个帖子写的方案蛮实用的,分 ...
- spring-mvc集成 swagger
问题1:spring-mvc集成 swagger, 配置好后界面 404, 原因: dispatcher-servlet.xml 文件中, 要在这上面 <!-- 启用spring mvc 注解 ...
- plaidctf-2016 Pwn试题小结
回顾了一下今年plaidctf Pwn部分的题目,感觉还是蛮有意思的,值得研究一下. 1.unix_time_formatter-76 最简单的一道题,考点是UAF.说是UAF但是其实根本就不算是真正 ...
- 洛谷P3387 【模板】缩点 题解
背景 今天\(loj\)挂了,于是就有了闲情雅致来刷\(luogu\) 题面 洛谷P3387 [模板]缩点传送门 题意 给定一个\(n\)个点\(m\)条边有向图,每个点有一个权值,求一条路径,使路径 ...
- Using MongoDB in C#
In order to use MongoDB in C#, you can import MongoDB C# Driver to your project. It's best to create ...
- chrome使用技巧(转)
原文:http://www.cnblogs.com/liyunhua/p/4544738.html 阅读目录 写在前面 快速切换文件 在源代码中搜索 在源代码中快速跳转到指定的行 使用多个插入符进行选 ...
- java okhttp 发送图片
@RequestMapping(value="/demo2", method=RequestMethod.POST) @ResponseBody public String dem ...
- 多线程十之CopyOnWriteArrayList源码分析
目录 简介 类结构 源码解析 构造方法 add(E e) add(int index, E element) get(int index) remove(int index) 迭代器Iterator遍 ...