Python批量處理CSV檔案

來源:互聯網
上載者:User

標籤:blog   java   os   io   檔案   for   ar   art   

#encoding: utf-8__author__ = ‘DELL‘import csvimport globimport datetimeimport sysimport osreload(sys)#中文錯誤sys.setdefaultencoding( "utf-8" )‘‘‘@author likehua    CSV批處理‘‘‘class BatchProcessCSV:    def __init__(self,inputfolder="c:\\input\\",outputfolder="c:\\output\\"):        self.inputfolder=inputfolder        self.outputfolder=outputfolder    #批處理    def doBatchAction(self):        startTime=datetime.datetime.now()        print(u"開始處理...")        if (os.path.exists(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"處理完成,耗時:%f秒"%(endTime-startTime).seconds)    #讀取一個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)        #讀取列 將字串轉為數組        column=csvfile.readline().split(",")        #print(column.index(‘App Release Date‘))        #print(column)        writer.writerow([‘Rank‘ ,‘Category‘, ‘Country ‘,‘App Name‘, ‘Value‘, ‘Unit‘ , ‘App Release Date‘, ‘Publisher Name‘, ‘Company Name‘, ‘Parent Company Name‘])        reader = csv.reader(csvfile)        #table = 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.writerow([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()

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.