Python operation Csv-xls-Perfect code

Source: Internet
Author: User

#Coding:utf-8#Import the appropriate moduleImportcsv,xlwt,sys,os,fnmatch,xlrd fromXlutils.copyImportCopy#maximum value for absolute data in XLS file and tabledefMax_excel (Excel): Rb1=Xlrd.open_workbook (Excel)#sheet0=rb1.sheets () [1]wb1=copy (RB1)#Sheet page can get the number of rows and columns by sheet name, but cannot write, the sheet obtained by subscript is writable, but the number of rows and columns cannot be obtainedSheet0=rb1.sheet_by_name ("Absolute") Sheet3=wb1.get_sheet (2) nrows1=sheet0.nrows ncols1=Sheet0.ncols forKinchRange (0,NCOLS1,): Sheet3.write (0,k,u'Fry dot%d'% (k+1)) cols=sheet0.col_values (k) Cmax=max (cols[1:]) Sheet3.write (1, K, Cmax) Wb1.save (Excel)#absolute value of data in an XLS filedefAbs_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.ncols forIinchRange (nrows): forJinchRange (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)#Save as XLS filedefEx_file (mycsvfile): CSVFile= Open (Mycsvfile,"RB")    #csvfile = open ("Test.csv", "RB")    #Create a new Excel fileMyexcel =XLWT. Workbook ()#New Sheet pageMysheet1= Myexcel.add_sheet ("Data") Mysheet2= Myexcel.add_sheet ("Absolute") Mysheet3= Myexcel.add_sheet ("MAX")    #get the file name of the CSVPortion =Os.path.splitext (mycsvfile)#read file information in CSVReader = Csv.reader (csvfile,dialect='Excel') L=0#get a single line of information through loops     forLineinchReader:r=0#get single cell information through a double loop         forIinchLine :#write Excel tables by double loopMysheet1.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, filesinchOs.walk (path): forFileNameinchfnmatch.filter (Files, fnexp):yieldos.path.join (root, filename)#Batch processingif __name__=="__main__": MyPath=raw_input ("Please enter a path:") Myfnexp='*.csv'    #Print 1     forFileNameinchiterfindfiles (mypath,myfnexp):#Print filenameex_file (filename)#abs_excel (filename)        #max_excel (filename)Raw_input ('Please enter to exit')

Finally, you can \py files via python pyinstaller--console--onefile absolute path. py command to produce the corresponding EXE file.

Python operation Csv-xls-Perfect code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.