-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPyBank.py
90 lines (72 loc) · 2.86 KB
/
PyBank.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Nov 14 23:48:32 2019
@author: claudedavid
"""
# First we'll import the os module
# This will allow us to create file paths across operating systems
import os
# Module for reading CSV files
import csv
# Make a path for the CSV file
budget= os.path.join("Resources", "budget_python.csv")
total_months = 0
total_pl = 0
value = 0
change = 0
dates = []
profits = []
# Opening and reading hte CSV file
with open(budget, newline='') as budgetcsvfile:
# CSV reader specifies delimiter and variable that holds contents
budgetcsvreader = csv.reader(budgetcsvfile, delimiter=',')
print(budgetcsvreader)
# Read the header row first (skip this step if there is now header)
budgetcsv_header = next(budgetcsvreader)
print(f"CSV Header: {budgetcsv_header}")
#Reading the first row (so that we track the changes properly)
first_row = next(budgetcsvreader)
total_months += 1
total_pl += int(first_row[1])
value = int(first_row[1])
#Going through each row of data after the header & first row
for row in budgetcsvreader:
# Keeping track of the dates
dates.append(row[0])
# Calculate the change, then add it to list of changes
change = int(row[1])-value
profits.append(change)
value = int(row[1])
#Total number of months
total_months += 1
#Total net amount of "Profit/Losses over entire period"
total_pl = total_pl + int(row[1])
#Greatest increase in profits
greatest_increase = max(profits)
greatest_index = profits.index(greatest_increase)
greatest_date = dates[greatest_index]
#Greatest decrease (lowest increase) in profits
greatest_decrease = min(profits)
worst_index = profits.index(greatest_decrease)
worst_date = dates[worst_index]
#Average change in "Profit/Losses between months over entire period"
avg_change = sum(profits)/len(profits)
#Displaying information
print("Financial Analysis")
print("---------------------")
print(f"Total Months: {str(total_months)}")
print(f"Total: ${str(total_pl)}")
print(f"Average Change: ${str(round(avg_change,2))}")
print(f"Greatest Increase in Profits: {greatest_date} (${str(greatest_increase)})")
print(f"Greatest Decrease in Profits: {worst_date} (${str(greatest_decrease)})")
#Exporing to .txt file
output = open("output.txt", "w")
line1 = "Financial Analysis"
line2 = "---------------------"
line3 = str(f"Total Months: {str(total_months)}")
line4 = str(f"Total: ${str(total_pl)}")
line5 = str(f"Average Change: ${str(round(avg_change,2))}")
line6 = str(f"Greatest Increase in Profits: {greatest_date} (${str(greatest_increase)})")
line7 = str(f"Greatest Decrease in Profits: {worst_date} (${str(greatest_decrease)})")
output.write('{}\n{}\n{}\n{}\n{}\n{}\n{}\n'.format(line1,line2,line3,line4,line5,line6,line7))