[Tutor] Help!

Chelsea G cegarcia0323 at gmail.com
Mon Jan 25 12:39:26 EST 2016


Hi,
I am trying to create a keyword search, so that someone can type in  a key
phrase and then the output be the value with the key. I have some code
already done but having some trouble getting it to work.
import csv
import json
import sys
from collections import defaultdict
from collections import Counter


class dictionary():
def __init__(self):
self.dict = defaultdict(list)
self.counted_dict = defaultdict(list)
self.grouped_dict = defaultdict(list)
self.other_dict = defaultdict(list)
self.final_dict = defaultdict(list)
self.total_dict = defaultdict(list)
self.keyword_dict = defaultdict(list)
def populate_dict(self, filename):
with open (filename, 'rb') as f:
reader = csv.reader(f)
next(reader, None)
for row in reader:
self.dict[row[2]].append(row[3])
def total_counts(self):
for key in self.dict.keys():
total = 0
b = Counter(self.dict[key])
for value in b:
total += b[value]
self.total_dict.update({key: str(total)})
def all_counts(self):
data_count = Counter()
for key in self.dict.keys():
self.counted_dict.update({key: Counter(self.dict[key])})
def grouped_counts(self):
for key in self.dict.keys():
total = 0
c = Counter(self.dict[key])
for value in c:
if c[value] >= 5:
self.grouped_dict.update({value: key + ': ' + str(c[value])})
elif c[value] <= 4:
total += c[value]
self.other_dict.update({key: 'other: ' + str(total)})
self.final_dict = self.grouped_dict, self.other_dict, self.total_dict

def keyword_items(defaultdict, lookup):
defaultdict = {' Tool Issue': ['Project Tool'], 'All catalogs missing or
not updating': ['Design Tool']}
for value, key in defaultdict.items():
for v in value:
if lookup in v:
return key
def json_output(self):
with open ('testing.txt', 'w') as text_file:
json.dump(self.final_dict, text_file, sort_keys = True, indent = 4)


More information about the Tutor mailing list