python - reading and writing files for a food processing company -
i working on python project food processing company trying calculate total sales year. python has read text file divided categories split commas. first category type of product, can cereal, chocolate candy etc produced company. second category brand of said product, example, kaptain krunch cereal or coco jam chocolate. third category sales last fiscal year(2014) , last category sales fiscal year(2015). note sales fiscal year 2015 calculated. 2014 has no use in program there. here how text file looks like. name product.txt
cereal,magic balls,2200,2344
cereal,kaptain krunch,3300,3123
cereal,coco bongo,1800,2100
cereal,sugar munch,4355,6500
cereal,oats n barley,3299,5400
sugar candy,pop rocks,546,982
sugar candy,lollipop,1233,1544
sugar candy,gingerbud,2344,2211
sugar candy,respur,1245,2211
chocolate,coco jam,3322,4300
chocolate,larkspur,1600,2200
chocolate,mighty milk,1234,2235
chocolate,almond berry,998,1233
condiments,peanut butter,3500,3902
condiments,hot sauce,1234,1560
condiments,jelly,346,544
condiments,spread,2334,5644
what looking add sales fiscal year 2015 products , total sales in 2015
the output should in written text file
total sales cereal in 2015 : {insert total number here}
total sales sugar candy in 2015 : {insert total number here}
total sales chocolate in 2015 : {insert total number here}
total sales condiments in 2015 : {insert total number here}
total sales company in 2015: {insert total products sold in 2015}
along that, should print grand total on python run screen in ide along text file.
total sales company in 2015: {insert total products sold in 2015}
here code. new python , reading , writing files can't if on right track.
product_file = "products.txt" report_file = "report.txt" def main(): #open file productfile = open(product_file, "r") reportfile = open(report_file, "w") # reading file prodata = extractdatarecord(productfile) product = prodata[0] category = prodata[1] saleslastyear = prodata[2] salesthisyear = prodata[3] #computing product = 0.0 product = salesthisyear productfile.close() reportfile.close() def extractdatarecord(infile) : line = infile.readline() if line == "" : return [] else : parts = line.rsplit(",", 1) parts[1] = int(parts[1]) return parts # start program. main()
the short version here you're doing wrong. never roll own parsing code if can it. i'd suggest taking @ the built-in csv
module, , trying using "contract out" csv parsing, letting focus on rest of logic.
simple rewrite , completed code csv
:
import collections import csv product_file = "products.txt" report_file = "report.txt" def main(): # easy way dictionary lookup defaults 0 categorycounts = collections.defaultdict(int) #open files using statements ensure they're closed # without need explicit call close, on exceptions open(product_file, newline='') productfile,\ open(report_file, "w") reportfile: pcsv = csv.reader(productfile) # sum sales product type letting csv parse # filter removes empty rows us; assume other rows complete category, brand, sales_lastyear, sales_thisyear in filter(none, pcsv): categorycounts[category] += int(sales_thisyear) # print categories in sorted order total sales category, sales in sorted(categorycounts.items()): print('total sales for', category, 'in 2015:', sales, file=reportfile) print('-'*80, file=reportfile) # separator line between categories , total # sum , print total sales both file , screen totalsales = sum(categorycounts.values()) print("total sales company in 2015:", totalsales, file=reportfile) print("total sales company in 2015:", totalsales) if __name__ == '__main__': main()
Comments
Post a Comment