-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcsv_splitter.py
62 lines (39 loc) · 1.35 KB
/
csv_splitter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import os
import helper
def split_csv(input_file, max_chunk_size=60000):
target_file_dir = os.path.dirname(input_file)
target_file_prefix = helper.get_name_without_extension(input_file)
current_chunk_size = 0
current_chunk_index = 1
header_str = ''
b_first = True
f_in = open(input_file, 'r')
f_out = None
for line in f_in:
if b_first:
# Read header for the first time alone and don't do anything else there
header_str = line
b_first = False
continue
if current_chunk_size == 0:
# Create new file with chunk index
target_file_name = target_file_prefix + '_' + str(current_chunk_index) + '.csv'
target_file_path = os.path.join(target_file_dir, target_file_name)
if f_out is not None:
f_out.close()
f_out = open(target_file_path, 'w')
# Write header
f_out.write(header_str)
# Write current line
f_out.write(line)
# Validate chunk size
current_chunk_size += 1
if current_chunk_size >= max_chunk_size:
current_chunk_size = 0
current_chunk_index += 1
f_in.close()
def main():
input_file = 'csv_data/BloombergVOTELEVEL_Touse.csv'
split_csv(input_file)
if __name__ == '__main__':
main()