Created
February 15, 2020 14:32
-
-
Save UweZiegenhagen/08885a0c08a6f23bd2c3855106a1522c to your computer and use it in GitHub Desktop.
Parsing Lexware Finance Manager / Quicken QIF files
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- coding: utf-8 -*- | |
| """ | |
| Created 20200215 | |
| @author: Uwe Ziegenhagen, ziegenhagen@gmail.com | |
| Initial version, needs heavy refactoring | |
| See https://github.com/jemmyw/Qif/blob/master/QIF_references for info | |
| """ | |
| from pprint import pprint | |
| class Transaction(): | |
| def __init__(self, date): | |
| self.date = date | |
| pass | |
| def date(self, date): # D | |
| self.date = date | |
| def amount(self, amount): # T | |
| self.amount = float(amount.replace(',','')) | |
| def cleared(self, flag): # C | |
| self.cleared = flag | |
| def reference(self, number): # N | |
| self.reference = number | |
| def payee(self, name): # P | |
| self.payee = name | |
| def description(self, text): # M | |
| self.description = text | |
| def category(self, text): # L | |
| self.category = text | |
| dateformat = '' | |
| autoswitch = False | |
| mode = 'default' | |
| classes = dict() | |
| categories = dict() | |
| klasse = '' | |
| temp_trans = None | |
| beschreibung = '' | |
| accounts = dict() | |
| transactions = [] | |
| def handle_option(line): | |
| global dateformat | |
| global autoswitch | |
| splits = line.split(':') | |
| if splits[1][:-1] == 'MDY': | |
| dateformat = 'M.D.YYYY' | |
| elif splits[1][:-1] == 'AutoSwitch': | |
| autoswitch = True # Indicates the accountlist | |
| def handle_other(line): | |
| global classes | |
| global klasse | |
| global accounts | |
| global categories | |
| global beschreibung | |
| if mode == 'class': | |
| if line.startswith('N'): | |
| klasse = line[1:-1] | |
| elif line.startswith('D'): | |
| beschreibung = line[1:-1] | |
| elif line.startswith('^'): | |
| classes.update({klasse : beschreibung}) | |
| elif mode == 'cat': | |
| if len(line)>3: | |
| klasse = line[1:-1] | |
| elif line[:-1] in ['I', 'E']: | |
| beschreibung = line[:-1] | |
| elif line.startswith('^'): | |
| categories.update({klasse : beschreibung}) | |
| elif mode == 'accounts': | |
| if line.startswith('N'): | |
| klasse = line[1:-1] | |
| elif line.startswith('T'): | |
| beschreibung = line[1:-1] | |
| elif line.startswith('D'): | |
| beschreibung = line[1:-1] | |
| elif line.startswith('^'): | |
| accounts.update({beschreibung : klasse}) | |
| elif mode == 'transactions': | |
| global temp_trans | |
| global transactions | |
| if line.startswith('D'): | |
| temp_trans = Transaction(line[1:-1]) | |
| elif line.startswith('T'): | |
| temp_trans.amount(line[1:-1]) | |
| elif line.startswith('C'): | |
| temp_trans.cleared(line[1:-1]) | |
| elif line.startswith('N'): | |
| temp_trans.reference(line[1:-1]) | |
| elif line.startswith('P'): | |
| temp_trans.payee(line[1:-1]) | |
| elif line.startswith('M'): | |
| temp_trans.description(line[1:-1]) | |
| elif line.startswith('L'): | |
| temp_trans.category(line[1:-1]) | |
| elif line.startswith('^'): | |
| transactions.append(temp_trans) | |
| with open('Quicken_h.QIF', encoding='cp1252') as file: | |
| for line in file: | |
| if line.startswith('!Option'): | |
| mode = 'option' | |
| handle_option(line) | |
| elif line.startswith('!Type:Class'): | |
| mode = 'class' | |
| elif line.startswith('!Type:Cat'): | |
| mode = 'cat' | |
| elif line.startswith('!Type:Bank'): | |
| mode = 'transactions' | |
| elif line.startswith('!Account'): | |
| if autoswitch == True: | |
| mode = 'accounts' | |
| elif line.startswith('!Clear:AutoSwitch'): | |
| mode = 'default' | |
| autoswitch = False | |
| elif line.startswith('!'): | |
| mode = 'other' | |
| print(line) | |
| else: | |
| handle_other(line) | |
| for t in transactions: | |
| print(t.amount) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment