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文件。

  • 相关阅读:
    git笔记
    初试linux,cp、rm、mv、file、umask等命令粗略使用方法
    Linux的rwx
    nano的简单笔记
    windows10访问ftp中文乱码怎么办?
    Windows 将FTP 映射到本地文件夹 --简化操作
    计算地址掩码
    Can't connect to MySQL server on localhost (10061)解决方法
    微信小程序开发入门学习(2):小程序的布局
    微信小程序开发入门学习(1):石头剪刀布小游戏
  • 原文地址:https://www.cnblogs.com/Skyyj/p/4764160.html
Copyright © 2011-2022 走看看