[Tutor] File extension change

Chelsea G cegarcia0323 at gmail.com
Tue Feb 2 12:46:07 EST 2016


Hi,
So what I am working on is taking a csv file and only taking 2 columns from
the spreadsheet and out putting that to a text file. Then taking those two
columns and organize them by product(key) and outputting the
description(values) that are associated. Some products have a lot of
duplicate descriptions and I am trying to get the counts of those. I have a
piece of code that takes anything greater then 5 and prints that and also
anything 4 or less goes into an 'other' category with the counts.So what I
am trying to do now is import a csv and change it to a text file with the
same naming convention as the csv. But I want the text file to have the
results from the json function. I want the text file that I am trying to
output to have the same name as the filename I am importing. The file that
I am inputting in the def populate_dict (usually the files are named
weekly_and the date...ex: weekly_20160102.csv) and then i want to run all
my code i have, but then in the def json_output instead of having the
filename which i have now is 'test.txt' I want to have weekly_20160102.txt
same naming convention as the csv file i am inputting. In my code I am soft
coding the filename so that the user is prompted to enter what file they
want to use.

import csvimport jsonimport sysimport osfrom collections import
defaultdictfrom collections import Counter

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)


    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])
		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		
				

    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


More information about the Tutor mailing list