forked from OasisLMF/OasisLMF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplit.py
76 lines (51 loc) · 1.81 KB
/
split.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env python3
import re
import numpy as np
import pandas as pd
import os
import json
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-dir", "--subdirectory", help="enter subdirectory name")
params = parser.parse_args()
subdir = params.subdirectory
os.chdir(subdir)
locationfile = pd.read_csv('location.csv')
accountfile = pd.read_csv('account.csv')
split_location = locationfile.groupby('FlexiLocUnit')
split_account = accountfile.groupby('FlexiAccUnit')
newpath = 'units'
if not os.path.exists(newpath):
os.makedirs(newpath)
cwd = os.getcwd()
for name, group in split_location:
sub_dir = os.path.join(newpath, name)
# loop through the groups and save to directories based on unique values
for name, group in split_location:
sub_dir = os.path.join(newpath, name)
if not os.path.exists(sub_dir):
os.mkdir(sub_dir)
group = group.drop(['FlexiLocUnit'], axis=1)
group.to_csv(sub_dir + "/location.csv", index=0)
for name, group in split_account:
sub_dir = os.path.join(newpath, name)
if not os.path.exists(sub_dir):
os.mkdir(sub_dir)
group = group.drop(['FlexiAccUnit'], axis=1)
group.to_csv(sub_dir + "/account.csv", index=0)
names = sorted([str(item[0]) for item in split_location])
# Function to sort fm string in Ascedning order
def ascedning(text):
return int(text) if text.isdigit() else text
def natural_keys(text):
return [ascedning(c) for c in re.split(r'(\d+)', text)]
names.sort(key=natural_keys)
# print(names)
units_dir = os.path.join(cwd, 'units')
if not os.path.exists(units_dir):
os.mkdir(units_dir)
with open(os.path.join(units_dir, 'units.txt'), "w") as txt_file:
names, groups = map(list, zip(*split_location))
names.sort(key=natural_keys)
for name in names:
txt_file.write(str(name) + '\n')