Python Batch processing CSV file

Source: Internet
Author: User

#encoding: utf-8__author__ = ' DELL ' import csvimport globimport datetimeimport sysimport osreload (SYS) # Chinese error sys.setdefaultencoding ("Utf-8") ' @author Likehua csv Batch ' class Batchprocesscsv:def __init__ (Self,inputfolde    R= "c:\\input\\", outputfolder= "c:\\output\\"): Self.inputfolder=inputfolder Self.outputfolder=outputfolder #批处理 def dobatchaction (self): Starttime=datetime.datetime.now () Print (u "start processing ...") if (Os.path.exi STS (Self.outputfolder) ==false): #pass os.makedirs (self.outputfolder) List_dirs = Os.walk (self                . inputfolder) for root, dirs, files in List_dirs: #print I for file in files: Otput=self.outputfolder+file self.readcsv2csv (self.inputfolder+file,otput) print (U "Running ... \ n ") Endtime=datetime.datetime.now () Print (U" processing completed, Time:%f sec "% (endtime-starttime). Secon--------- DS) #读取一个csv提取部分信息生成新的CSV def readcsv2CSV (self,inputfile,outputfile): With open (Inputfile, ' RB ') as Csvfile:o=open (OutputFile, "WB") #解决csv浏览乱码问题        O.write (' \XEF\XBB\XBF '); Writer=csv.writer (o) #读取列 Convert the string to array Column=csvfile.readline (). Split (",") #print (Column.index (' App rele ASE Date ') #print (column) Writer.writerow ([' Rank ', ' Category ', ' country ', ' App Name ', ' Value ', ' Unit ', ' Ap P Release Date ', ' Publisher name ', ' Company name ', ' Parent company name '] reader = Csv.reader (csvfile) #tab Le = reader[0] #Rank, Category, Store, Device, Type, Country, Period,version, app_id, App_name, Value, Unit, Value_ Type, AppUrl, App_iap, App_category, App_device, Current_price, App_release_date, publisher_id, Publisher_Name, CompanyName, Parentcompanyname, appnameunified, Appfranchise, Unifiedappid, Appfranchiseid, CompanyID, Parentcompanyid for row in Reader:lenth=len (row) if Lenth>10:writer.write Row ([Row[column. Index ("Rank")],row[column.index ("Category")],row[column.index ("Country")],row[column.index ("App Name")],row[ Column.index ("Value")],row[column.index ("Unit")],row[column.index ("App Release Date")],row[column.index (" Publisher name ")],row[column.index (" Company name ")],row[column.index (" Parent company Name ")]) #processif __name__== "__main__": Csvprocess=batchprocesscsv ("c:\\input\\", "e:\\output\\") csvprocess.dobatchaction ()

 

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.