Skip to content

Instantly share code, notes, and snippets.

@firobeid
Created May 27, 2023 02:16
Show Gist options
  • Select an option

  • Save firobeid/dc0cfc1e2cb7c41c44949fbdcdfea53d to your computer and use it in GitHub Desktop.

Select an option

Save firobeid/dc0cfc1e2cb7c41c44949fbdcdfea53d to your computer and use it in GitHub Desktop.
# Read the csv and convert it into a list of dictionaries
with open(file_to_load) as financial_data:
reader = csv.reader(financial_data)
# Read the header row
header = next(reader)
# Extract first row to avoid appending to net_change_list
first_row = next(reader)
total_months = total_months + 1
total_net = total_net + int(first_row[1])
prev_net = int(first_row[1])
for row in reader:
# Track the total
total_months = total_months + 1
total_net = total_net + int(row[1])
# Track the net change
net_change = int(row[1]) - prev_net
prev_net = int(row[1])
net_change_list = net_change_list + [net_change]
month_of_change = month_of_change + [row[0]]
# Calculate the greatest increase
if net_change > greatest_increase[1]:
greatest_increase[0] = row[0]
greatest_increase[1] = net_change
# Calculate the greatest decrease
if net_change < greatest_decrease[1]:
greatest_decrease[0] = row[0]
greatest_decrease[1] = net_change
# Calculate the Average Net Change
net_monthly_avg = round(sum(net_change_list) / len(net_change_list),2)
# Export the results to text file
with open(file_to_output, "w") as txt_file:
txt_file.write(f"Financial Analysis\n")
txt_file.write(f"----------------------------\n")
txt_file.write(f"Total Months: {total_months}\n")
txt_file.write(f"Total: ${total_net}\n")
txt_file.write(f"Average Change: ${net_monthly_avg}\n")
txt_file.write(f"Greatest Increase in Profits: {greatest_increase[0]} (${greatest_increase[1]})\n")
txt_file.write(f"Greatest Decrease in Profits: {greatest_decrease[0]} (${greatest_decrease[1]})\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment