[Tutor] help!

Chelsea G cegarcia0323 at gmail.com
Tue Jan 26 13:05:27 EST 2016


Hi,
I am working on a python script to automate reporting. And I am working on
creating a keyword search. For example, if I want to search for the word
Tool in the value and see what keys are associated with that. So say the
value I have to search is Tool World and I want to know what key is
associated with Tool World I search Tool World and it comes up with several
results like missing or not updating and catalog issue. I have the basic
code for it figured out but I have created my own dictionary for it called
mydict and put a few key and values in but I want my code to search the csv
file that I am importing and then take the info I am getting from the
search results and put it in its own text file. Also, I dont want to have
to create the mydict line with the keywords I want to be able to type in a
value like Tool and search through the csv file and then output the results
to a text file. I have attached my code.
-------------- next part --------------
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.search_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
			
	mydict = {'Project Tool Issue': ['CPO Project Tool'], 'All catalogs missing or not updating': ['20/20 Design Tool']}
	def search(mydict, lookup):
		for key, value in mydict.iteritems():
			for v in value:
				if lookup in v:
					 #return key
					print key
					
	search(mydict, 'Tool')
		
			
	# def txt_output(self, filename):
		# a = filename.split('.')
		# self.txt_output = a + '.txt'
		# print a
	
	
		
		
			
	
	def json_output(self):
		with open ('scripttesting.txt', 'w') as text_file:
			json.dump(self.final_dict, text_file, sort_keys = True, indent = 4)


More information about the Tutor mailing list