-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_names.py
71 lines (62 loc) · 2 KB
/
get_names.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
import sys
import math
import xlrd
import json
import re
class Names:
#----------------------------------------------------------------------------------------------------------------------------------------------------
#reads sample data dictionary and extracts name info. creates a new dictionary mapping from sample_id to name only
@staticmethod
def get_names(dic):
counter = 0
name_dic = {}
name_list = []
for key in dic.keys():
values = dic[key]
name_vals = []
for value in values:
name = value[1]
name_vals.append(name)
name_list.append(name)
name_dic[key] = name_vals
print_visual(name_dic, 'meteorite_names.txt')
return name_dic
#dictionary names had to be manually edited in a txt file, so this function prints the dic to a file in a way that is easy to edit
@staticmethod
def print_visual(dic, filename):
with open(filename, 'w') as f:
for k,v in dic.items():
str_ = str(k) + " : " + str(v[0]) + '\n'
f.write(str_);
#after the names have been manually edited, this file reads the new name dictionary in and cuts out whitespace and stuff.
@staticmethod
def read_names(file_):
name_dic = {}
with open(file_, 'r') as f:
lines = f.readlines()
for line in lines:
curr = line.rstrip('\n').split(":")
key = curr[0]
val = curr[1]
if "!" in val:
name_dic[key] = ""
elif "!" not in val:
val = val.rstrip().lstrip()
key = key.rstrip().lstrip()
name_dic[key] = val
print name_dic
return name_dic
@staticmethod
def export(dic, filename):
with open(filename, "w") as output:
output.write(str(dic))
'''
Main method
'''
@staticmethod
def run():
#Names.get_names(filtered_sample_data_dic) #-- uses filtered dictionary from analyze-data.py
name_dic = Names.read_names("meteorite_names.txt") #name dictionary
#Names.export(name_dic, "names_to_classify.txt")
run()
#-----------------------------------------------------------------------------------------------------------------------------------------------