Python batch process CSV files,

Source: Internet
Author: User

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 ()


Related Article

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: 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.