splitting one csv into multiple files in python

前端 未结 10 2133
执念已碎
执念已碎 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:54

    A simple Python 3 solution with Pandas that doesn't cut off the last batch

    def to_csv_batch(src_csv, dst_dir, size=30000, index=False):
    
        import pandas as pd
        import math
        
        # Read source csv
        df = pd.read_csv(src_csv)
        
        # Initial values
        low = 0
        high = size
    
        # Loop through batches
        for i in range(math.ceil(len(df) / size)):
    
            fname = dst_dir+'/Batch_' + str(i+1) + '.csv'
            df[low:high].to_csv(fname, index=index)
            
            # Update selection
            low = high
            if (high + size < len(df)):
                high = high + size
            else:
                high = len(df)
    

    Usage example

    to_csv_batch('Batch_All.csv', 'Batches')
    

提交回复
热议问题