Python Forum

Full Version: Analyzing yearly expenses (CSV file) from debit card
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I am quite new with programming in Python and now I have started my (kind of) 'first' project in Python. I want to analyse my yearly expenses and I think pandas is the best way to give a clear overview with a chart.

The query:
  • I have a yearly overview of expenses in a CSV file (see attached). This is an example, not my 'real' expenses Tongue
  • The lay-out of the csv file ins't clear, so I want to change it. Preferred is have all the columns 'Alligned left' and the first row (headers) should be Bold.
  • The output of the chart should visualize my expenses monthly. In the end I want to have an overview in which month the expenses are high and in which months the expenses are relative low.
  • If the analysis is done and the chart is made. I want to save the file with _MODIFIED added to the original file.

I tried starting this project with open and edit a CSV file using the following code:
import csv
from pathlib import Path
from tkinter.filedialog import askopenfilename
import sys
 
 
def read_csv_file(filename):
    parts = list(filename.parts)
    parts[-1] = f"{filename.stem}Modified{filename.suffix}"
    parts[0] = ''
    outfilename = Path(f"{'/'.join(parts)}")
 
    print(f"new output file name: {outfilename}")
 
    with filename.open() as fp, outfilename.open('w') as fout:
        crdr = csv.reader(fp, delimiter=',')
        cwrtr = csv.writer(fout, delimiter=',')
        for row in crdr:
            print(row)
            # Modify row as desired here
            cwrtr.writerow(row)
 
def get_filename():
    badcount = 0
 
    while(True):
        try:
            if badcount > 2:
                print("Three strikes and your out!")
                sys.exit(-1)
            filename = Path(askopenfilename(filetypes=[("CSV files","*.csv")]))
            break
        except TypeError:
            badcount += 1
            print(f"bad filename, try again")
         
    return filename
 
 
if __name__ == '__main__':
    read_csv_file(get_filename())
I have tried to code this project in Python, but after three weeks I just have 10 lines of code..... I just don't know where to start I guess.

CSV file:https://ibb.co/5rqR1YQ
I couldn't upload a .csv file, so instead I have added the picture above.

The amounts of the expenses are examples, so I haven't spent €986,- euros at the swimming pool Big Grin

Hopefully someone can help!
Are you trying to build a pivot table? pandas has support for them, though I must admit I haven't use it before.

Formatting cells (e.g. changing fonts to bold) , assuming you are using Excel to display the csv, can be done with openpyxl.
(Dec-13-2020, 02:29 PM)palladium Wrote: [ -> ]Are you trying to build a pivot table? pandas has support for them, though I must admit I haven't use it before.

Formatting cells (e.g. changing fonts to bold) , assuming you are using Excel to display the csv, can be done with openpyxl.

I am not very familiar with pivot tables, I was assuming a chart was reasonable for this kind of demanding.

Yes I am using Excel to display the csv file. Openpyxl is only for reading Excel xlsx, xlsm, xltx and xltm files I thought?
(Dec-13-2020, 02:42 PM)Python_User Wrote: [ -> ]Yes I am using Excel to display the csv file. Openpyxl is only for reading Excel xlsx, xlsm, xltx and xltm files I thought?

It can do formatting too:
https://openpyxl.readthedocs.io/en/stable/styles.html

(Dec-13-2020, 02:42 PM)Python_User Wrote: [ -> ]I am not very familiar with pivot tables, I was assuming a chart was reasonable for this kind of demanding.

Openpyxl does charts too: https://openpyxl.readthedocs.i/oen/stabl...ction.html

I must say I haven't used any of these features myself, I just came across these whilst reading the docs.
Quote:It can do formatting too:
https://openpyxl.readthedocs.io/en/stable/styles.html

Quote:Openpyxl does charts too: https://openpyxl.readthedocs.i/oen/stabl...ction.html

I must say I haven't used any of these features myself, I just came across these whilst reading the docs.

Aha, I think I got some homework to do this weekend ;).

Thank you palladium for the links, I will dive into them during this weekend.