Use this Python 3 program :
#!/usr/bin/env python3 import binascii import csv import os.path import sys from tkinter.filedialog import askopenfilename, askdirectory from tkinter.simpledialog import askinteger def split_csv_file(f, dst_dir, keyfunc): csv_reader = csv.reader(f) csv_writers = {} for row in csv_reader: k = keyfunc(row) if k not in csv_writers: csv_writers[k] = csv.writer(open(os.path.join(dst_dir, k), mode='w', newline='')) csv_writers[k].writerow(row) def get_args_from_cli(): input_filename = sys.argv[1] column = int(sys.argv[2]) dst_dir = sys.argv[3] return (input_filename, column, dst_dir) def get_args_from_gui(): input_filename = askopenfilename( filetypes=(('CSV', '.csv'),), title='Select CSV Input File') column = askinteger('Choose Table Column', 'Table column') dst_dir = askdirectory(title='Select Destination Directory') return (input_filename, column, dst_dir) if __name__ == '__main__': if len(sys.argv) == 1: input_filename, column, dst_dir = get_args_from_gui() elif len(sys.argv) == 4: input_filename, column, dst_dir = get_args_from_cli() else: raise Exception("Invalid number of arguments") with open(input_filename, mode='r', newline='') as f: split_csv_file(f, dst_dir, lambda r: r[column-1]+'.csv')
Save it as split-csv.py and run it from Explorer or from the line command.
For example, to split superuser.csv based on column 1 and write the output files to dstdir use:
python split-csv.py data.csv 1 dstdir
If you run it without arguments, the Tkinter-based GUI will offer you to select the input file, column (index based on 1) and the destination directory.
ref
source share