|
| 1 | +###This code parses tabular data *automatically using the headers for keys |
| 2 | +###TL;DR Tabular data to list of dictionaries ready for json |
| 3 | + |
| 4 | +import itertools |
| 5 | +import collections |
| 6 | + |
| 7 | + |
| 8 | +###TODO: dynamic import |
| 9 | +###TODO: Automatic test for headers |
| 10 | +###TODO: Automatic expansion on lines that "aren't perfect" |
| 11 | +###TODO: dynamic values for str_match or at least user defineable |
| 12 | +###TODO: dynamic values for headers if duplicates |
| 13 | +def find(s, ch): |
| 14 | + return [i for i, ltr in enumerate(s) if ltr == ch] |
| 15 | + |
| 16 | +with open("desktop\\SSIDSECURITY2.txt","r") as f: |
| 17 | + list_arrays = f.readlines() |
| 18 | + |
| 19 | +def freq_check(str_match,list_arrays): |
| 20 | + list_occurances = [find(i,str_match) for i in list_arrays] |
| 21 | + return list_occurances |
| 22 | + |
| 23 | +def get_max_len(list_arrays): |
| 24 | + longest = len(max(list_arrays,key=len)) |
| 25 | + return longest |
| 26 | + |
| 27 | +def get_most_common(list_arrays): |
| 28 | + x = itertools.chain.from_iterable(list_arrays) |
| 29 | + most_common_char = collections.Counter(itertools.chain.from_iterable(x)).most_common()[0][0] |
| 30 | + return most_common_char |
| 31 | + |
| 32 | +str_match = get_most_common(list_arrays) |
| 33 | +list_occurances = freq_check(str_match,list_arrays) |
| 34 | +longest = get_max_len(list_arrays) |
| 35 | + |
| 36 | +###Creates a list of tuples that dictate most common occurances of str_match |
| 37 | +elements = sorted(collections.Counter(itertools.chain.from_iterable(list_occurances)).items(), key=lambda item: (-item[1], item[0])) |
| 38 | +###Be able to tell what the max occurance is |
| 39 | +max_occurance = elements[0][1] |
| 40 | + |
| 41 | +###Create a list from the common str_match |
| 42 | +slicer = [] |
| 43 | +[slicer.append(list(i)) for i in elements if i[1] == max_occurance] |
| 44 | + |
| 45 | +###FANCY *ISH* HERE |
| 46 | +###enumerate over list of space stops |
| 47 | +###Basically so we have a left and right bounding area |
| 48 | +stops = [] |
| 49 | +for idx,element in enumerate(slicer): |
| 50 | + ###get the next element of the list |
| 51 | + nxt_element = slicer[(idx + 1) % len(slicer)][0] |
| 52 | + ###appeend the two elements to the list |
| 53 | + stops.append([element[0],nxt_element]) |
| 54 | +stops[len(stops)-1][1] = longest |
| 55 | +stops[0][0] = 0 |
| 56 | + |
| 57 | + |
| 58 | +cols = [] |
| 59 | +for i in stops: |
| 60 | + for h in list_arrays: |
| 61 | + cols.append(h[i[0]:i[1]].strip()) |
| 62 | + |
| 63 | +idx_list = [] |
| 64 | +for idx,i in enumerate(cols): |
| 65 | + if i == '': |
| 66 | + idx_list.append(idx) |
| 67 | + |
| 68 | +###Similar to lines 35 bounding areas |
| 69 | +comp_list = [] |
| 70 | +for idx,element in enumerate(idx_list): |
| 71 | + nxt_element = idx_list[(idx+1)%len(idx_list)] |
| 72 | + comp_list.append([element,nxt_element]) |
| 73 | + |
| 74 | +comp_list.insert(0,[0,idx_list[0]]) |
| 75 | +comp_list.pop() |
| 76 | + |
| 77 | +info = [] |
| 78 | +for i in comp_list: |
| 79 | + info.append(cols[i[0]:i[1]]) |
| 80 | + |
| 81 | +for idx,i in enumerate(info): |
| 82 | + if idx > 0: |
| 83 | + info[idx].pop(0) |
| 84 | + |
| 85 | +###The code below creates a sorted list of all elments in the list |
| 86 | +###We can then group those items from the [0] element creating a list of tuples |
| 87 | +###What it looks like is (0,'BSSID','BSSID'),...,(1,'BSSID','DE:AD:BE:EF') |
| 88 | +###TODO: This code is slick, I question it's sustainability |
| 89 | +x_sorted_list = sorted(itertools.chain.from_iterable([[(idx,i[0],h) for idx,h in enumerate(i)] for i in info])) |
| 90 | +x_list = [] |
| 91 | +###The code groups list by first element |
| 92 | +###https://stackoverflow.com/questions/58403206/how-to-group-lists-inside-a-list-by-first-element |
| 93 | +###Then appends that to the x_list |
| 94 | +for i in [list(item[1]) for item in itertools.groupby(x_sorted_list, key=lambda x: x[0])]: |
| 95 | + x_obj = {} |
| 96 | + for h in i: |
| 97 | + x_obj[h[1]] = h[2] |
| 98 | + x_list.append(x_obj) |
| 99 | +x_list.pop(0) |
0 commit comments