-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv_operations.py
47 lines (39 loc) · 1.49 KB
/
csv_operations.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
import csv
import os
# Path to store expenses
FILE_NAME = "expenses.csv"
# Function to initialize the csv file if it doesn't exist
def init_file():
if not os.path.exists(FILE_NAME):
with open(FILE_NAME, mode="w", newline="") as file:
writer = csv.writer(file)
writer.writerow(["Date", "Category", "Description", "Amount"])
# Function to add an expense
def add_expense(date, category, description, amount):
with open(FILE_NAME, mode="a", newline="") as file:
writer = csv.writer(file)
writer.writerow([date, category, description, amount])
# Function to view all expenses
def view_expenses():
with open(FILE_NAME, mode="r") as file:
reader = csv.reader(file)
for row in reader:
print(row)
# Function to update an expense
def update_expense(expense_index, new_date, new_category, new_description, new_amount):
expenses = []
# Read all the current expenses
with open(FILE_NAME, mode="r") as file:
reader = csv.reader(file)
expenses = list(reader)
# Check if the index is valid
if 0 < expense_index < len(expenses):
# Update the selected expense
expenses[expense_index] = [new_date, new_category, new_description, new_amount]
# Write the updated list of expenses back to the file
with open(FILE_NAME, mode="w", newline="") as file:
writer = csv.writer(file)
writer.writerows(expenses)
return True
else:
return False