python操作csv-xls完善后的代码

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

#coding:utf-8#导入相应模块importcsv,xlwt,sys,os,fnmatch,xlrdfromxlutils.copyimportcopy#对xls文件中的绝对值数据求最大值并列表defmax_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.ncolsforkinrange(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数据求绝对值defabs_excel(excel):#PRint "3"rb=xlrd.open_workbook(excel)#sheet=rb.sheets()[0]wb=copy(rb)

sheet= rb.sheet_by_name("data")#print sheetsheet2=wb.get_sheet(1)

nrows=sheet.nrows

ncols=sheet.ncolsforiinrange(nrows):forjinrange(ncols):

w=sheet.cell(i,j).valueif(i==0):

sheet2.write(i,j,w)else:

v=float(w)

sheet2.write(i,j,abs(v))

wb.save(excel)

max_excel(excel)#另存为xls文件defex_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#通过循环获取单行信息forlineinreader:

r=0#通过双重循环获取单个单元信息foriinline:#通过双重循环写入excel表格mysheet1.write(l,r,i)

r+=1l+=1myexcel.save(portion[0]+".xls")

excel= portion[0]+".xls"#print portion[0]+".xls"abs_excel(excel)defiterfindfiles(path, fnexp):forroot, dirs, filesinos.walk(path):forfilenameinfnmatch.filter(files, fnexp):yieldos.path.join(root, filename)#批量处理if__name__=="__main__":

mypath=raw_input("Please enter a path:")

myfnexp='*.csv'#print 1forfilenameiniterfindfiles(mypath,myfnexp):#print filenameex_file(filename)#abs_excel(filename)#max_excel(filename)raw_input ('please enter to exit')

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

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航