From e715c36c2b6047f30407412cc1af6b6afb12fd59 Mon Sep 17 00:00:00 2001 From: René 'Necoro' Neumann Date: Mon, 10 May 2010 19:58:02 +0200 Subject: Add converter --- convert.py | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 convert.py (limited to 'convert.py') diff --git a/convert.py b/convert.py new file mode 100644 index 0000000..c1cd770 --- /dev/null +++ b/convert.py @@ -0,0 +1,74 @@ +#!/usr/bin/python + +import elixir +from model import * + +import csv +from decimal import Decimal +from datetime import datetime + +elixir.create_all() + +# categories +d = dict((x, Category(name = x)) for x in ("Essen", "Kneipe", "Fahrkarten", "Kino", "Mittag", "Sonstiges")) +d["Mittag"].parent = d["Essen"] + +session.commit() + +# single expenses +with open("kosten.csv") as f: + dialect = csv.Sniffer().sniff(f.read(1024)) + f.seek(0) + for row in csv.DictReader(f, dialect = dialect): + s = SingleExpense() + + # expense + s.expense = Decimal(row["betrag"]) + + # date + s.year, s.month, s.day = map(int, row["datum"].split('-')) + + # description + s.description = unicode(row["kommentar"], 'utf-8') + + # category + if row["nahrung"] == "1": + s.category = d["Essen"] + elif row["kneipe"] == "1": + s.category = d["Kneipe"] + elif row["fahrkarte"] == "1": + s.category = d["Fahrkarten"] + elif row["kino"] == "1": + s.category = d["Kino"] + elif row["mittag"] == "1": + s.category = d["Mittag"] + else: + s.category = d["Sonstiges"] + +session.commit() + +# const expenses +with open("konstkosten.csv") as f: + dialect = csv.Sniffer().sniff(f.read(1024)) + f.seek(0) + for row in csv.DictReader(f, dialect = dialect): + c = ConstExpense() + + # expense + c.expense = Decimal(row["betrag"]) + + # date + c.start = datetime.strptime(row["start"], "%Y%m") + c.end = datetime.strptime(row["ende"], "%Y%m") + + # description + c.description = unicode(row["name"], 'utf-8') + + # interval + types = {'m': 1, 'q': 3, 'h': 6, 'j': 12} + c.months = types[row["typ"]] + + # category: just 'Sonstiges' for the moment + c.category = d["Sonstiges"] + +session.commit() -- cgit v1.2.3-70-g09d2