-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSplit_CSV.py
More file actions
28 lines (23 loc) · 1018 Bytes
/
Split_CSV.py
File metadata and controls
28 lines (23 loc) · 1018 Bytes
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
# To Split a .csv File to multiple .csv files
# Applications: When we have Computational Power Constraints, we can use .csv file in parts.
# Also, to view large .csv files but with "not so good GPU/CPU" and many more.
def split_csv_file(filename, lines_per_file):
with open(filename, 'r') as file:
header = file.readline()
file_count = 1
for i, line in enumerate(file, start=1):
if i % lines_per_file == 1:
if file_count > 1:
out_file.close()
out_filename = f"{filename.rsplit('.', 1)[0]}_{file_count}.csv"
out_file = open(out_filename, 'w')
out_file.write(header)
file_count += 1
out_file.write(line)
out_file.close()
# Add the Path of File which is to be Split
file_path = ""
# Add the Rows per After split .csv file would contain
rows_per_file = 100
# Calling the Function to Split .csv to Multiple .csv files
split_csv_file(file_path, rows_per_file)