Expense Tracker in Python – Level 1

Expense Tracker in Python - Level 1

Expense tracking is a common task used in every industry. In this post, we’re going to build a simple expense tracker in Python for exercise. This Python expense tracker will simply track your expenses in a CSV file. By the end of this tutorial, you’ll have a Python expense tracking program that shows you your expenses and allows you to add to the tracker.

In this post, we’ll cover:

  • Reading Expenses from a CSV File in Python
  • Writing to Your Python Expense Tracker
  • Seeing and Adding to Your Expenses
  • Summary of How to Build a Python Expense Tracker

Find this project on GitHub.

Reading Expenses from a CSV File in Python

We could start with a function to read from or write to the CSV file that we’re using to track our expenses. For this tutorial, I chose to start with reading expenses. First things first, we import the csv library. 

Our read_expenses function doesn’t need any parameters. We’re hard-coding in the name of our expense file. This means we need to make sure that we run this program in the same folder that we have the program code and the CSV file.

First, we try to open the expenses.csv file and create a CSV reader. Next, we declare an empty list that represents the expenses. We use the CSV reader to add all the rows of the expense tracker to the list of expenses. 

Now, we print out our expenses. Notice the comment there that denotes the way that the expenses are written. This column format has to be followed for both the expense reader and writer. We show the user that on a certain date, they spend the cost on the category.

If there is no expenses.csv file, we simply print out that the file doesn’t exist and move on.

import csv
 
def read_expenses():
      try:
       with open("expenses.csv", "r") as f:
           csv_reader = csv.reader(f, delimiter=",")
           expenses = []
           for row in csv_reader:
               expenses.append(row)
       # expenses come in the columns of date (0), category (1), price (2)
       for line in expenses:
           print(f"On {line[0]}, {line[2]} was spent on {line[1]}")
   except:
       print("No Expense Tracker File Exists Yet")

Writing to Your Python Expense Tracker

Now that we’ve created a function to read from our expense tracker, let’s create a function to write to it. Our write_expenses function executes a while loop while we are reporting expenses. To do this, we start with a reporting variable that we set to True and then open up the expenses file in append mode and create a CSV writer with it.

While we are reporting, we ask the user to input the date, category, and cost. We write this data that the user input into the expense tracker. Once we write the data in, we ask the user if they are done reporting. If they are, then we set our reporting variable to False to end the while loop. Finally, we close the file.

def write_expenses():
   reporting = True
   f = open("expenses.csv", "a")
   expense_writer = csv.writer(f, delimiter=",")
   while reporting:
       date = input("What date was the expense incurred? ")
       category = input("What category is the expense for? ")
       cost = input("How much money did you spend? ")
       expense_writer.writerow([date, category, cost])
       end = input("If you are done inputting expenses, type \"end\" ")
       if end == "end":
           reporting = False
 
   f.close()

Seeing and Adding to Your Expenses

Now it’s time to put the reading and writing functions together. First we tell the user that we’re going to show them the current state of the expense report. Then, we call the read_expenses function we made earlier. 

After showing them the current expense tracker (or the fact that it doesn’t exist), we ask the user if they want to report expenses. If they do, then we call the write_expense function we made.

print("Current state of expense report: ")
read_expenses()
report = input("Would you like to report expenses?(y/n) ")
if report == "y":
   write_expenses()

Starting from no expense tracker file, this is what an expense report would look like when we run the program.

After we put in the initial expenses, we can run the program again and see that all our expenses were saved and we can continue to report expenses if we’d like.

Summary of How to Build a Python Expense Tracker

In this post we learned how to build a simple CSV expense tracker in Python. We made two functions to encapsulate the reading and writing to a CSV functionality of our expense tracker. Then, we wrote a few lines of Python to run the program as a script. Our Python expense tracker shows you the date you made the expenditure, the category that you spent on, and the amount you spent.

Leave a Reply

%d bloggers like this: