Skip to content

Commit 1064545

Browse files
committed
Add splitLargeCSV.py utility script
1 parent fd87f89 commit 1064545

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

Networking-Test-Kit/splitLargeCSV.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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

Comments
 (0)