[Tutor] Help with error

Joel Goldstick joel.goldstick at gmail.com
Tue Feb 2 13:07:42 EST 2016


First, Please try to be more creative with your message subject line.
Help, isn't -- helpfull

Second.  Don't paraphrase the error, copy it from your actual terminal
screen and paste it here.

See below

On Tue, Feb 2, 2016 at 10:37 AM, Chelsea G <cegarcia0323 at gmail.com> wrote:

> Hi,
> When I run my code I am getting an error it says "Attribute Error:
> Dictionary instance has no attribute 'search'. So the whole idea for my
> code is to input a csv file and output results that we want. This specific
> piece of code def search is to search the csv file for any keyword like
> 'issues' and output those results into a text file. I just need some help
> with figuring out how to fix the error I am receiving.
> This is my functions:
>
> import csvimport jsonimport sysfrom collections import defaultdictfrom
> collections import Counter
> data = defaultdict(list)
>
> upper_limit = 5
> lower_limit = 4
> 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 all_counts(self):
>         data_count = Counter()
>         for key in self.dict.keys():
>             self.counted_dict.update({key: Counter(self.dict[key])})
>
>     def total_counts(self):
>         for key in self.dict.keys():
>             total = 0
>             b = Counter(self.dict[key])
>             for value in b:
>                 total += b[value]
>                 new_list = str(total)
>                 #self.total_dict.update({key: 'Total count for this
> application: ' + str(total)})
>                 #self.total_dict.update({key: str(total)})
>             self.total_dict[key].append(new_list)
>
>     def grouped_counts(self):
>         for key in self.dict.keys():
>             total = 0
>             c = Counter(self.dict[key])
>             for value in c:
>                 if c[value] >= upper_limit:
>                     new_list = value, str(c[value])
>                     self.grouped_dict[key].append(new_list)
>                 elif c[value] <= lower_limit:
>                     total += c[value]
>                     self.other_dict.update({key: 'other: ' + str(total)})
>
>         for d in (self.grouped_dict, self.other_dict, self.total_dict):
>             for key, value in d.iteritems():
>                 self.final_dict[key].append(value)
>
>     def json_output(self):
>         with open('test.txt', 'w') as text_file:
>             json.dump(self.final_dict, text_file, sort_keys = True, indent
> = 4)
>
> Your search method below is indented so as to be a function within
json_output.  Remove the indentation so that it lines up with your other
class methods.


>         def search(self, filename):
>                 with open(filename, 'r') as searchfile,
> open('weekly_test.txt', 'w')
> as search_results_file:
>                         for line in searchfile:
>                                 if 'PBI 43125' in line:
>                                         print >>search_results_file, line
>
>
>
> And then I have another python file where I import my functions and
> run the results. Here is the code for that:
>
> import functions2
>
> week_choice = raw_input("Previous Week or Current Week?")if
> week_choice == "current week":
>         data = functions2.dictionary()
>         filename = raw_input("What file do you want to use?")
>         data.populate_dict(filename)
>         data.total_counts()
>         data.grouped_counts()
>         data.json_output()
>         data.search(filename)
>         elif week_choice == "previous week":
>         previous_data = functions2.dictionary()
>         filename = raw_input("What file do you want to use?")
>         previous_data.populate_dict(filename)
>         previous_data.total_counts()
>         previous_data.grouped_counts()
>         previous_data.json_output()
>         previous_data.search(filename)
>         else:
>         print "That choice is not correct"
>
>
> It says the error is with data.search(filename).. Not sure how to get
> this working.
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>



-- 
Joel Goldstick
http://joelgoldstick.com/stats/birthdays


More information about the Tutor mailing list