Files
PythonFlaskAusgaben/files/modify_csv.py
jonnybravo c0cfe686d1
Some checks failed
Gitea Actions Demo / Explore-Gitea-Actions (push) Failing after -56s
switch to ras-dan-01
2025-03-31 15:21:28 +02:00

71 lines
2.0 KiB
Python
Executable File

#! /usr/bin/env python3.13
import csv
import os
from datetime import date
default_csv = "ausgaben_month/ausgaben.csv"
def create_csv_file(csv_path=default_csv):
if not os.path.exists(csv_path):
print(csv_path, "wird angelegt !!")
to_write_header = ["datum", "ausgaben", "beschreibung"]
with open(csv_path, "w") as wfile:
writer = csv.writer(wfile)
writer.writerow(to_write_header)
return "INFO :", csv_path, "wurde angelegt"
else:
return "INFO :", csv_path, "existiert bereits"
def read_csv_file(csv_file=default_csv):
if os.path.exists(csv_file):
read_csv_file_ausgabe = []
with open(csv_file, "r") as ocsv:
read_csv_full = csv.DictReader(ocsv)
for csv_lines in read_csv_full:
read_csv_file_ausgabe.append(
{
"datum": csv_lines["datum"],
"ausgabe": csv_lines["ausgaben"],
"beschreibung": csv_lines["beschreibung"],
}
)
return read_csv_file_ausgabe
else:
create_csv_file()
read_csv_file()
def write_csv_file(
csv_file=default_csv, date_time=date.today(), betrag=float, select_dist="Essen"
):
import_list = [date_time, betrag, select_dist]
with open(csv_file, "a") as wfile:
writer = csv.writer(wfile)
writer.writerow(import_list)
def sum_all():
sum_rech = float(0)
for val_csv in read_csv_file():
sum_rech = float(sum_rech) + float(val_csv["ausgabe"])
return round(sum_rech, 2)
if __name__ == "__main__":
print(sum_all())
# Apply multiple filters
# print(create_csv_file())
# print(read_csv_file())
# for i in read_csv_file():
# query_str = i["datum"][:-3]
# if query_str == "2025-01":
# print(i["datum"], " ist in 2025-01")
# if i["datum"] < date("2025", "01", "01"):
# print(True)
# webbrowser.open("file://" + os.path.realpath("ausgabe.html"))