Lab 10: File I/O

For this lab, you will prepare a single python program. I am providing you with some starter code:
  1. lab10.py

Turning in your work

Be sure to turn in whatever you have finished, at the end of the lab session. You may continue to work until midnight, and submit your work again.

Late penalties: 1 day late: -5%, 2 days: -10%, -3 days: -20%.

To turn your work in, go to mycourses.unh.edu, find CS414, open the syllabus, and click on Lab 10. Then click Submit Assignment, click Choose file, find the file that you worked on, and upload it.

Background info

In case you haven't done so already, go to the course website, http://cs.unh.edu/~cs414, scroll to the bottom, and visit the three links that talk about loops and file I/O. READ THEM CAREFULLY.

Exercises

Download the starting program, and the input file:

Then do these exercises:
  1. The program currently opens the input file, and reads all its lines. Add code that opens an OUTPUT file called 'money.txt':
                out_handle = open('money.txt', 'w')
              
    and writes all the lines into it:
                for line in lines:
                    out_handle.write(line)
              
    then closes the file: out_handle.close().

    Check the file money.txt . If you did everything right, it should look just like the original file growth.txt .

  2. Now we will do some processing. Look at growth.txt, and notice that each line has three fields, separated by commas.

    Instead of just writing a line to the output file, do this:

    Check the file money.txt . If you did everything right, it should look just like the original file growth.txt, although there may be an extra comma at the end of each line (if so, don't worry; that's OK).

  3. The first line of the file is the "header". It has the names of the three fields: Year, Balance, and Explanation. Let's add another field, Income, between Balance and Explanation.

    Write the first line separately, with an extra field:

                  out_handle.write('Year,Bank Balance,Income,Explanation\n')
                  for line in lines[1:] :
                     fields = line.split(',')
                     ...
                
    The fields list has 3 strings. We want to do some computations, so let's turn some of them into numbers, after fields is created:
                  balance = float(fields[1])
                
    Now, we want to compute the income, i.e., the difference between balances. For example in 1995, the income was 1100 - 1080 = 20. In 1996, the income was 1000000.

    To do this, start with prev_balance = 1000, and do the following, before writing the fields:

                  income = balance - prev_balance
                  fields.insert(2, income)
                  prev_balance = balance
                
    Now fields has three strings, and one float. Run the program. It should crash, because of out_handle.write(field + ',') . The write() function expects a string, but the third field is a float.

    Change that line:

                  out_handle.write(str(field) + ',')
                
    and the program should run correctly.

  4. Add another field to the file: the growth rate. Several parts of the program will change:

  5. Compute averages for the balance, the income, and the growth rate columns. You will have to create three totals: total_balance, total_income, and total_growth. Initialize these to zero.

    As you process each line, add each value into its total:

                  total_balance += balance
                
    and similarly for income and growth.

    Then, after processing all the lines, obtain the average values: divide these totals by (len(lines) - 1).

    Finally, add one (or more) final write() calls that output the word Averages in the first field, then the three averages.

  6. Repeat the previous exercise, but now compute the minimums and maximums for the three columns Balance, Income, and Rate.

    Add two lines to the output file, one for the minimums, and one for the maximums.