1
+
2
+ import os
3
+ import csv
4
+
5
+
6
+ def split (filehandler , delimiter = ',' , row_limit = 4000000 ,
7
+ output_name_template = 'output_%s.csv' , output_path = '.' , keep_headers = True ):
8
+
9
+ reader = csv .reader (filehandler , delimiter = delimiter )
10
+ current_piece = 1
11
+ current_out_path = os .path .join (
12
+ output_path ,
13
+ output_name_template % current_piece
14
+ )
15
+ current_out_writer = csv .writer (open (current_out_path , 'w' , newline = '' ), delimiter = delimiter )
16
+ current_limit = row_limit
17
+ headers = []
18
+ if keep_headers :
19
+ for i in range (5 ):
20
+ headerRow = next (reader );
21
+ headers .append (headerRow )
22
+ current_out_writer .writerow (headerRow )
23
+
24
+ for i , row in enumerate (reader ):
25
+ if i + 1 > current_limit :
26
+ current_piece += 1
27
+ current_limit = row_limit * current_piece
28
+ current_out_path = os .path .join (
29
+ output_path ,
30
+ output_name_template % current_piece
31
+ )
32
+ current_out_writer = csv .writer (open (current_out_path , 'w' , newline = '' ), delimiter = delimiter )
33
+ if keep_headers :
34
+ for headerRowI in headers :
35
+ current_out_writer .writerow (headerRowI )
36
+ print (headerRowI )
37
+ print (i )
38
+ current_out_writer .writerow (row )
39
+
40
+ split (open ('OpenBCI-RAW-2021-04-05_21-49-12.txt' , 'r' ));
0 commit comments