-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconversion.py
More file actions
53 lines (41 loc) · 1.56 KB
/
conversion.py
File metadata and controls
53 lines (41 loc) · 1.56 KB
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
import csv
import os
import sys
from collections import defaultdict
def split_file(in_csv_path:str, out_csv_path=None):
disease = defaultdict(list)
with open(in_csv_path, mode='r', newline='', encoding='utf-8') as file:
reader = csv.reader(file)
header = next(reader)
for row in reader:
disease[row[0]].append(row)
for disease_name, rows in disease.items():
if out_csv_path is None:
in_csv_path = in_csv_path.replace('\\', '/')
path = "/".join(in_csv_path.split('/')[:-1])
path = "SeparatedData/" + path
if not os.path.exists(path):
os.makedirs(path)
path = path + "/"
output_path = f"{path}{disease_name}.csv"
else:
output_path = out_csv_path
with open(output_path, mode='w', newline='', encoding='utf-8') as file:
writer = csv.writer(file)
writer.writerow(header)
writer.writerows(rows)
def split_all_data():
if len(sys.argv) < 2:
print(f'please include an argument of where the folder is located')
inFolder = sys.argv[1]
filesToParse = []
if os.path.exists(inFolder):
print("Locating files...")
for root, dirs, files in os.walk(inFolder):
for name in files:
if name[-4:] == '.csv': # Only parse csv files
filesToParse.append(f'{root}/{name}'.replace('\\', '/'))
print(f'files to parse: {filesToParse[:5]}')
for file in filesToParse:
split_file(file)
split_all_data()