-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit_unit.py
More file actions
60 lines (44 loc) · 1.58 KB
/
split_unit.py
File metadata and controls
60 lines (44 loc) · 1.58 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
54
55
56
57
58
59
60
import numpy as np
import pandas as pd
import os
import json
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
import re
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')