Untitled

mail@pastecode.io avatar
unknown
python
2 years ago
1.3 kB
3
Indexable
Never
import csv
import os

def split_file(filename, output_path, split_rows=10000):
    if filename == '':
        print("please input filename")
        return
    workspace = output_path
    if workspace == '':
        workspace = os.getcwd()

    first_row = ''
    with open(filename, newline='' , encoding='utf-8') as f:
        reader = csv.reader(f)
        first_row = next(reader)

        i = j = 0
        for row in reader:
            if i % split_rows == 0:
                j += 1

            csv_path = os.path.join(workspace, 'filename_part_{}.csv'.format(j))
            
            if not os.path.exists(csv_path):
                with open(csv_path, 'w', newline='') as file:
                    csvwriter = csv.writer(file)
                    csvwriter.writerow(first_row)
                    csvwriter.writerow(row)
            else:
                with open(csv_path, 'a', newline='') as file:
                    csvwriter = csv.writer(file)
                    csvwriter.writerow(row)
            i += 1
    print('总计{}行数据,已分割为{}个文件'.format(i,j))
        

if __name__ == "__main__":
    filename = ""
    output_path = ""
    split_file(filename, output_path,40)