zoukankan      html  css  js  c++  java
  • Python Application

    问题背景

    班长总是辛苦滴,是不是就得统计什么东西,每个人写好之后发到邮箱,一个个下载过来不说,还得一个个打开,复制黏贴,重复劳动呀,我们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 = 0
    10 for num in xrange(len(filenames)):
    11     #find all .xls using regular expression
    12     if re.match(r'\d*\.xls$', filenames[num]):
    13         #read the excel
    14         file = xlrd.open_workbook(filenames[num])
    15         #get the sheet of the excel
    16         sheetR = file.sheets()[0]
    17         #gain the row num and column num
    18         nrows = sheetR.nrows
    19         ncols = sheetR.ncols
    20         for i in xrange(nrows):
    21             #remove the title except it is the first row
    22             #and if there is no title in excel, we should read directly
    23             if i == 0 and count != 0 and nrows != 1:
    24                 continue
    25             for j in xrange(ncols):  
    26                 #write every cell into the sheet
    27                 sheetW.write(count, j, sheetR.cell(i, j).value)
    28             count += 1
    29 newFile.save('newFile.xls')
    View Code

    小结

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

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

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

    改进

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

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

  • 相关阅读:
    Json介绍与Ajax技术
    Java学习笔记(二一)——Java 泛型
    谏牲口TT十思疏
    Java学习笔记(二十)——Java 散列表_算法内容
    Java学习笔记(十九)——Java 日志记录 AND log4j
    读书笔记(三)——《山楂树之恋》敢爱吗?
    Java学习笔记(十八)——Java DTO
    Java学习笔记(十七)——java序列化
    Java学习笔记(十六)——Java RMI
    Java学习笔记(十五)——javadoc学习笔记和可能的注意细节
  • 原文地址:https://www.cnblogs.com/chuanlong/p/3085544.html
Copyright © 2011-2022 走看看