To complete this lab, you must download the Loans.csv file from BlackBoard. This file might become corrupted if you open it in Excel. If you would like to view the file before processing it, you should open it in a text editor like Notepad or Notepad++. If prompted, do not save the file after opening it. If your file becomes corrupted, or if you are getting an unexplained error, like ‘ValueError: could not convert string to float:’, you should try replacing the file on your machine with a fresh version downloaded from BlackBoard.

Programming with Python

Lab VII – Future Value File

Submission: FirstNameLastNameLab7.py file

Description

To complete this lab, you must download the Loans.csv file from BlackBoard. This file might become corrupted if you open it in Excel. If you would like to view the file before processing it, you should open it in a text editor like Notepad or Notepad++. If prompted, do not save the file after opening it. If your file becomes corrupted, or if you are getting an unexplained error, like ‘ValueError: could not convert string to float:’, you should try replacing the file on your machine with a fresh version downloaded from BlackBoard.

  1. Write a comment on the first line with your first name, last name, and Lab 7, e.g.:

#Michael Deamer Lab 7

 

  1. Save the file as FirstNameLastNameLab6.py, e.g. py, and submit the file after completion.

 

  1. First, we need to create a string variable representing our file name and path. Please use the variable names sourceFileName and destinationFilename:

 

sourceFileName = ‘C:/Users/michael.deamer/Desktop/Python IO/Loans.csv’

destinationFileName = ‘C:/Users/michael.deamer/Desktop/Python IO/LoansWithFutureValue.csv’

 

Note:

The strings should represent where these files can be found on your machine.

 

  1. We can now open the files using a with statement:

 

with open(sourceFileName, ‘r’, encoding=’utf-8′) as sourceFile, open(destinationFileName, ‘a’, encoding=’utf-8′) as destinationFile:

 

Note:

The sourceFile is opened in read mode and the destinationFile is opened in the append mode.

 

  1. Let’s read the first line of this file and print the result:

 

line = sourceFile.readline()

print(line)

 

Result:

Loan ID, First Name, Last Name, Loan Principle, Annual Interest Rate, Term

 

Note:

The print statement in this step was to test that the code is working and should be removed.

 

  1. Based on what is printed in Step 5, we know the first line is the column header and should be skipped. We can do this by adding a second readline() directly beneath the line in Step 5:

 

line = sourceFile.readline()

print(line)

 

Result:

eca7d5db-eeff-49c1-a585-e3017bf3cb2b,Ibby,Hubbert,$485754.42,2.01%,8

Note:

Again, the print statement should be removed after we know the code is working.

 

  1. Because we are reading from a csv, it is possible that each line includes a line break. We can remove that using the replace function:

 

line = line.replace(‘\n’, ”)

 

  1. Based on the results from step 6, each line contains 6 values separated by commas. We can use the split() function to change this string to a list data type:

lineList = line.split(‘,’)

 

  1. Now we have a list with six values. The first item in the list is the Loan Id. The second is the recipient’s first name. The third is the last name. The fourth is the loan principle. The fifth is the annual rate of interest. And the sixth is the term of the loan. We can separate each of these into variables by referencing their respective indexes in the list:

        loanID = lineList[0]

        firstName = lineList[1]

        lastName = lineList[2]

        loanPrinciple = lineList[3]

        interest = lineList[4]

        term = lineList[5]

 

  1. We now have all the values we need to calculate the future value of the loan. To conduct the calculation, we can use the function created in Homework 5. Copy the code below, and paste it before the with statement from Step 4:

 

def calculateFutureValue(p, r, t, n = 365):

    pFloat = float(p.replace(‘$’, ”).replace(‘,’, ”))

    rFloat = float(r.replace(‘%’, ”))*.01

    tFloat = float(t)

    a = pFloat*(1 + rFloat/n)**(n*tFloat)

    return a

 

  1. Now call this function using the variables created in Step 9:

 

fV = calculateFutureValue(loanPrinciple, interest, term)

 

  1. Now that we have processed the first line of data, we need to add a loop to read each line of the csv one-by-one. To do this, we can add a while statement after the code created in Step 6:

 

while line != ”:

 

Note:

The condition is that the loop should repeat until the line variable equals an empty string. This works because there is no empty line in our source csv. The first empty line indicates that there is no more data.

The lines of code from Steps 7 to 11 should appear inside this while loop.

 

  1. The last line of this while loop should read the next line of the csv:

 

line =sourceFile.readline()

 

  1. Now we are processing every line of the csv and calculating the future value of each loan. We are ready to write this information to a new file. However, before we do so, we need to add the column headers to our destination file. We can write the column headers after opening the destination file but before the while loop:

 

destinationFile.write(‘Loan ID, First Name, Last Name, Loan Principle, Annual Interest Rate, Term, Future Value\n’)

 

  1. Now, we can write each line to the destination file:

 

destinationFile.write(loanID+’,’+firstName+’,’+lastName+’,’+loanPrinciple+’,’+interest+’,’+term+’,’+str(fV)+’\n’)

 

Note:

This line of code should appear as the second to last line in the while loop

Calculate the price of your order

550 words
We'll send you the first draft for approval by September 11, 2018 at 10:52 AM
Total price:
$26
The price is based on these factors:
Academic level
Number of pages
Urgency
Basic features
  • Free title page and bibliography
  • Unlimited revisions
  • Plagiarism-free guarantee
  • Money-back guarantee
  • 24/7 support
On-demand options
  • Writer’s samples
  • Part-by-part delivery
  • Overnight delivery
  • Copies of used sources
  • Expert Proofreading
Paper format
  • 275 words per page
  • 12 pt Arial/Times New Roman
  • Double line spacing
  • Any citation style (APA, MLA, Chicago/Turabian, Harvard)

Our guarantees

Delivering a high-quality product at a reasonable price is not enough anymore.
That’s why we have developed 5 beneficial guarantees that will make your experience with our service enjoyable, easy, and safe.

Money-back guarantee

You have to be 100% sure of the quality of your product to give a money-back guarantee. This describes us perfectly. Make sure that this guarantee is totally transparent.

Read more

Zero-plagiarism guarantee

Each paper is composed from scratch, according to your instructions. It is then checked by our plagiarism-detection software. There is no gap where plagiarism could squeeze in.

Read more

Free-revision policy

Thanks to our free revisions, there is no way for you to be unsatisfied. We will work on your paper until you are completely happy with the result.

Read more

Privacy policy

Your email is safe, as we store it according to international data protection rules. Your bank details are secure, as we use only reliable payment systems.

Read more

Fair-cooperation guarantee

By sending us your money, you buy the service we provide. Check out our terms and conditions if you prefer business talks to be laid out in official language.

Read more
error: Content is protected !!