博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python Application
阅读量:7049 次
发布时间:2019-06-28

本文共 1592 字,大约阅读时间需要 5 分钟。

问题背景

班长总是辛苦滴,是不是就得统计什么东西,每个人写好之后发到邮箱,一个个下载过来不说,还得一个个打开,复制黏贴,重复劳动呀,我们IT人,怎么能做这些如此简单的事,所以就想着简化点事,至少我下载过来,不用一个个打开复制黏贴,于是乎写个了python脚本。

问题描述

将一些列以数字命名的xls里面的内容,合并到一个xls中

代码

1 import xlrd, xlwt, os, sys, re 2 #get the catalogue 3 filenames = os.listdir(os.getcwd()) 4 #new excel for write 5 newFile = xlwt.Workbook() 6 #add sheet, then u can control sheet of the excel 7 sheetW = newFile.add_sheet("sheet") 8 #count the num of the rows 9 count = 010 for num in xrange(len(filenames)):11     #find all .xls using regular expression12     if re.match(r'\d*\.xls$', filenames[num]):13         #read the excel14         file = xlrd.open_workbook(filenames[num])15         #get the sheet of the excel16         sheetR = file.sheets()[0]17         #gain the row num and column num18         nrows = sheetR.nrows19         ncols = sheetR.ncols20         for i in xrange(nrows):21             #remove the title except it is the first row22             #and if there is no title in excel, we should read directly23             if i == 0 and count != 0 and nrows != 1:24                 continue25             for j in xrange(ncols):  26                 #write every cell into the sheet27                 sheetW.write(count, j, sheetR.cell(i, j).value)28             count += 129 newFile.save('newFile.xls')
View Code

小结

1、用xlrd,xlwt实现读写,可以试试其他的

2、考虑到了内容可能存在不同,比如有些xls里没有title

3、Last but not the least,一个个cell读,效率未免低下,而且没有判读是否有重复的元素

改进

1、针对这小程序,可以让这更“智能”(判读重复,格式化输出),更“高效”(一行行写)

2、针对这背景,可以实现一个web端的,班长添加title,学生登录,填写信息,班长直接download,是不是更强大

 

转载于:https://www.cnblogs.com/chuanlong/archive/2013/05/18/3085544.html

你可能感兴趣的文章
【树莓派】Linux 测网速及树莓派源
查看>>
Java用户线程和守护线程
查看>>
[TypeScript] Use the never type to avoid code with dead ends using TypeScript
查看>>
Javascript 与 SPA单页Web富应用
查看>>
SpringMVC之访问静态文件
查看>>
【java设计模式】之 模板方法(Template Method)模式
查看>>
【踩坑速记】MIUI系统BUG,调用系统相机拍照可能会带给你的一系列坑,将拍照适配方案进行到底!...
查看>>
小米手机会不会更好
查看>>
atitit.Sealink2000国际海运信息管理系统
查看>>
android面试总结01 activity生命周期
查看>>
Java 实现策略(Strategy)模式
查看>>
Python文本爬虫实战
查看>>
leetcode:Gray Code
查看>>
IDEA+PHP+XDebug调试配置
查看>>
Jenkins
查看>>
Ubuntu离线安装Sogou拼音(附老版本安装&输入法自启动)
查看>>
springmvc结合base64存取图片到mysql
查看>>
深度学习主机环境配置: Ubuntu16.04+GeForce GTX 1080+TensorFlow
查看>>
linux 抓包 tcpdump 简单应用
查看>>
mongodb官网文档阅读笔记:与写性能相关的几个因素
查看>>