zoukankan      html  css  js  c++  java
  • python操作csv-xls完善后的代码

    #coding:utf-8
    #导入相应模块
    import csv,xlwt,sys,os,fnmatch,xlrd
    from xlutils.copy import copy
    
    #对xls文件中的绝对值数据求最大值并列表
    def max_excel(excel):
        rb1=xlrd.open_workbook(excel)
        #sheet0=rb1.sheets()[1]
        wb1=copy(rb1)
        #sheet页通过sheet名称得到的才能获取行数和列数,但是不能进行写操作,通过下标获得的sheet可以进行写操作,但是不能获得行数和列数
        sheet0=rb1.sheet_by_name("Absolute")
        sheet3=wb1.get_sheet(2)  
        nrows1=sheet0.nrows
        ncols1=sheet0.ncols   
        for k in range(0,ncols1,):
            sheet3.write(0,k,u'炸点%d' %(k+1))            
            cols = sheet0.col_values(k)
            cmax=max(cols[1:])       
            sheet3.write(1,k, cmax)  
        wb1.save(excel)
    
    #对xls文件中的data数据求绝对值
    def abs_excel(excel):
        #print "3"
        rb=xlrd.open_workbook(excel)
        #sheet=rb.sheets()[0]
        wb=copy(rb)
        sheet = rb.sheet_by_name("data")
        #print sheet
        sheet2=wb.get_sheet(1)   
        nrows=sheet.nrows
        ncols=sheet.ncols  
        for i in range(nrows):
            for j in range(ncols):
                w=sheet.cell(i,j).value
                if (i==0):
                    sheet2.write(i,j,w)
                else:
                    v = float(w)
                    sheet2.write(i,j,abs(v))
        wb.save(excel) 
        max_excel(excel)
    
    #另存为xls文件
    def ex_file(mycsvfile):
        csvfile = open(mycsvfile,"rb")
        #csvfile = open("test.csv","rb")
        #新建excel文件
        myexcel = xlwt.Workbook()
        #新建sheet页
        mysheet1= myexcel.add_sheet("data")
        mysheet2= myexcel.add_sheet("Absolute")
        mysheet3= myexcel.add_sheet("MAX")
        #获取csv的文件名
        portion = os.path.splitext(mycsvfile)
        #读取csv中文件信息
        reader = csv.reader(csvfile,dialect='excel')
        l = 0
        #通过循环获取单行信息
        for line in reader:
            r = 0
            #通过双重循环获取单个单元信息
            for i in line:
                #通过双重循环写入excel表格
                mysheet1.write(l,r,i)
                r+=1
            l+=1        
        myexcel.save(portion[0]+".xls")
        excel = portion[0]+".xls"
        #print portion[0]+".xls"
        abs_excel(excel)
                   
    def iterfindfiles(path, fnexp):
        for root, dirs, files in os.walk(path):
            for filename in fnmatch.filter(files, fnexp):
                yield os.path.join(root, filename)
        
    #批量处理
    if __name__=="__main__":
        mypath=raw_input("Please enter a path:")
        myfnexp='*.csv'
        #print 1
        for filename in iterfindfiles(mypath,myfnexp):
            #print filename
            ex_file(filename)
            #abs_excel(filename)
            #max_excel(filename)
        raw_input ('please enter to exit')

    最后可以通过 python pyinstaller --console --onefile  绝对路径py文件.py  命令生产成对应的exe文件。

  • 相关阅读:
    css的三种特性
    css选择器
    margin:0 auto 与 text-align:center 的区别
    JS如何实现点击页面内任意的链接均加参数跳转
    css和js带参数(形如.css?v=与.js?v= 或 .css?version=与.js?version=)
    移动端页面前端设计随笔整理
    理解:Before和:After伪元素
    时下流行的css3页面纵向滑动效果
    webapp网页调试工具Chrome Devtools
    做手机web半年遇到的问题及解决方法
  • 原文地址:https://www.cnblogs.com/Skyyj/p/4764160.html
Copyright © 2011-2022 走看看