Developers Planet

Jake Meyer February 2016

Calculate totals by reading from a text file in Python

I am trying to write a program in python in which we have to add the numbers from different categories and sub-categories. The program is about a farmer's annual sale of produce from his farm. The text file from where we have to read from has 4 categories. The first category is the type of product for example Vegetables, Fruits, condiments. The second category tells us about the type of product we have, for example Potatoes, Apples, Hot Sauce. The third category tells us about the sales in 2014 and the fourth category tells us about the sales in 2015. In this program, we only have to calculate the totals from the 2015 numbers. The 2014 numbers are present in the text file but are irrelevant.

Here is how the text file looks like

``````PRODUCT,CATEGORY,2014 Sales,2015 Sales
Vegetables,Potatoes,4455,5644
Vegetables,Tomatoes,5544,6547
Vegetables,Peas,987,1236
Vegetables,Carrots,7877,8766
Vegetables,Broccoli,5564,3498
Fruits,Apples,398,4233
Fruits,Grapes,1099,1234
Fruits,Pear,2342,3219
Fruits,Bananas,998,1235
Fruits,Peaches,1678,1875
Condiments,Peanut Butter,3500,3902
Condiments,Hot Sauce,1234,1560
Condiments,Jelly,346,544
Condiments,Ketchup,3321,3655
Condiments,Olive Oil,3211,2344
``````

What we are looking to do is to add the sales for 2015 by products and then the total sales for everything in 2015.

The output should look something like this in the written text file:

Total sales for Vegetables in 2015 : {Insert total number here}

Total sales for Fruits in 2015 : {Insert total number here}

Total sales for Condiments in 2015 : {Insert total number here}

Total sales for the farmer in 2015: {Insert total for all the products sold in 2015}

Along with that, it should also print the grand total on the Python run screen in the IDE along with the text file:

Total sales for the farmer in 2015: {I

Garrett R February 2016

You have a csv file so you should probably use pythons built-in csv module to parse the file. The DictReader class turns every line into a dictionary with the key being the header. If your csv file was called `product_sales.csv`, the following code would work.

``````import csv

product_dict = {}
cat_dict = {}
with open('product_sales.csv', 'r') as f:
cat = line['CATEGORY']
product = line['PRODUCT']
sales_15 = int(line['2015 Sales'])
if cat in cat_dict:
cat_dict[cat] += sales_15
else:
cat_dict[cat] = sales_15
if product in product_dict:
product_dict[product] += sales_15
else:
product_dict[product] = sales_15

Total = sum(cat_dict.values())
print product_dict
print cat_dict
print Total
``````

Hugh Bothwell February 2016

Your code looks like a good start; here are a few pointers:

• it is a good idea to use `with` when opening files because it guarantees they will get closed properly. Instead of

``````productFile = open(PRODUCT_FILE, "r")
# do something with the file
productFile.close()
``````

you should do

``````with open(PRODUCT_FILE, "r") as product_file:
# do something with the file
# the file has been closed!
``````
• you only call `proData = extractDataRecord(productFile)` once (ie you get the header line but none of the data). You could put it in a `while` loop, but it is much more idiomatic to iterate directly on the file, ie

``````for line in product_file:
product, category, _, sales = line.split(',')
sales = int(sales)
# now do something with the values!
``````

(using `_` as a variable name is shorthand for "I don't care about this value")

• you can use a `dict` to keep track of products and total sales for each,

``````product_sales = {}
``````

then in the `for` loop,

``````product_sales[product] = product_sales.get(product, 0) + sales
``````

If you could `from collections import defaultdict`, this becomes even simpler:

``````product_sales = defaultdict(int)

product_sales[product] += sales
``````
• once you have processed the entire file, you need to report on the results like

``````all_sales = 0
for product, sales in product_sales.items():
# write the sales for this product
all_sales += sales
# print all_sales
``````