splitting one csv into multiple files in python

前端 未结 10 2143
执念已碎
执念已碎 2020-12-05 07:28

I have a csv file of about 5000 rows in python i want to split it into five files.

I wrote a code for it but it is not working

import codecs
import c         


        
10条回答
  •  北荒
    北荒 (楼主)
    2020-12-05 07:45

    I have modified the accepted answer a little bit to make it simpler

    Edited: Added the import statement, modified the print statement for printing the exception. @Alex F code snippet was written for python2, for python3 you also need to use header_row = rows.__next__() instead header_row = rows.next(). Thanks for pointing out.

    import os
    import csv
    def split_csv_into_chunks(file_location, out_dir, file_size=2):
        count = 0
        current_piece = 1
    
        # file_to_split_name.csv
        file_name = file_location.split("/")[-1].split(".")[0]
        split_file_name_template = file_name + "__%s.csv"
        splited_files_path = []
    
        if not os.path.exists(out_dir):
            os.makedirs(out_dir)
        try:
            with open(file_location, "rb") as csv_file:
                rows = csv.reader(csv_file, delimiter=",")
                headers_row = rows.next()
                for row in rows:
                    if count % file_size == 0:
                        current_out_path = os.path.join(out_dir,
                                                        split_file_name_template%str(current_piece))
                        current_out_writer = None
    
                        current_out_writer = csv.writer(open(current_out_path, 'w'), delimiter=",")
                        current_out_writer.writerow(headers_row)
                        splited_files_path.append(current_out_path)
                        current_piece += 1
    
                    current_out_writer.writerow(row)
                    count += 1
            return True, splited_files_path
        except Exception as e:
            print("Exception occurred as {}".format(e))
            return False, splited_files_path
    

提交回复
热议问题