Python batch process CSV files,
# Encoding: UTF-8 _ author _ = 'Dell 'import csvimport globimport datetimeimport sysimport osreload (sys) # Chinese error sys. setdefaultencoding ("UTF-8") ''' @ author likehua CSV batch processing ''' class BatchProcessCSV: def _ init _ (self, inputfolder = "c: \ input \ ", outputfolder =" c: \ output \ "): self. inputfolder = inputfolder self. outputfolder = outputfolder # batch processing def doBatchAction (self): startTime = datetime. datetime. now () print (u "Start processing... ") 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 "processed, time consumed: % f seconds" % (endTime-startTime ). seconds) # Read the extracted information of a csv file to generate a new CSV def readcsv2csv (self, inputfile, outputfile): with open (inputfile, 'rb') as csvfile: o = open (outputfile, "wb") # solve the csv browsing garbled problem o. write ('\ xEF \ xBB \ xbf'); writer = csv. writer (o) # convert the string into an array column = csvfile. readline (). split (",") # print (column. index ('app Release date') # print (column) writer. writerow (['rank ', 'category', 'country ', 'app name', 'value', 'unit', 'app Release date', 'her her 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, region, Publisher_ID, Publisher_Name, CompanyName, ParentCompanyName, appnameuniied, AppFranchise, region, region, CompanyID, category 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 ()