title: Calculate totals by reading from a text file in Python
body: <p>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. </p>
  
  <p>Here is how the text file looks like</p>
  
  <pre><code>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,Spread,2334,5644
  Condiments,Ketchup,3321,3655
  Condiments,Olive Oil,3211,2344
  </code></pre>
  
  <p>What we are looking to do is to add the sales for 2015 by products and then the total sales for everything in 2015.</p>
  
  <p>The output should look something like this in the written text file:</p>
  
  <blockquote>
    <p>Total sales for Vegetables in 2015 : {Insert total number here}</p>
    
    <p>Total sales for Fruits in 2015 : {Insert total number here}</p>
    
    <p>Total sales for Condiments in 2015 : {Insert total number here}</p>
    
    <hr>
    
    <p>Total sales for the farmer in 2015: {Insert total for all the
    products sold in 2015}</p>
  </blockquote>
  
  <p>Along with that, it should also print the grand total on the Python run screen in the IDE along with the text file:</p>
  
  <blockquote>
    <p>Total sales for the farmer in 2015: {Insert total for all the
    products sold in 2015}</p>
  </blockquote>
  
  <p>Here is my code. I am new to Python and reading and writing files so I can't really say if I am on the right track.</p>
  
  <pre><code>PRODUCT_FILE = "products.txt"
  REPORT_FILE = "report.txt"
  
  def main():
      #open the file
      productFile = open(PRODUCT_FILE, "r")
      reportFile = open(REPORT_FILE, "w")
  
      # reading the 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 the program.
  main()
  </code></pre>