Path: blob/main/finance/ktoblzcheck-data/files/patch-src_switzerland.py
27950 views
- Adapt to SIX Group converting their bankdata to CSV (semicolon separated).1- Don't use codecs.open(); deprecated in Python 3.14.23--- src/switzerland.py.orig 2025-05-15 11:18:20 UTC4+++ src/switzerland.py5@@ -24,12 +24,11 @@ KMyMoney6@author: Christian David7"""89-import sqlite310-import codecs11import argparse12-import os13+import csv14+import sqlite31516-def createTable():17+def create_table():18""" Create table structure19"""20cursor = db.cursor()21@@ -45,59 +44,65 @@ def createTable():22)23db.commit()2425-26-def processFile(fileName):27+def process_file(filename):28""" Fills the database with institutions saved in fileName29"""30+ with open(filename, 'r', newline='') as institutes_file:31+ reader = list(csv.reader(institutes_file, delimiter=';'))32+ rows = reader[1:]3334- rowsInserted = 035- cursor = db.cursor()36- cursor.execute("BEGIN")37+ bank_map = {row[0]: {"bic": row[14], "name": f"{row[8]} ({row[12]})"} for row in rows}3839- def existCode(bankCode, bic):40- cursor.execute("SELECT bankcode,bic FROM institutions WHERE bankcode = ? and bic = ?",(bankCode,bic,))41- row_exist = cursor.fetchone()42- if row_exist is None:43- return False44+ to_insert = []45+ for row in rows:46+ bankcode = row[0].zfill(5)4748- return True49+ # Non-concatenated "parent" entries50+ if row[2] == 'N':51+ bic = row[14]52+ name = f"{row[8]} ({row[12]})"53+ # Some bankcodes are concatenated onto other bankcodes without the BIC or other54+ # institution info on their line, so we must get these from the parent entry55+ else:56+ parent_bankcode = row[3]57+ parent_info = bank_map.get(parent_bankcode)58+ if parent_info:59+ bic = parent_info["bic"]60+ name = parent_info["name"]61+ else:62+ continue6364- def submitInstitute(bankCode, bankName, bic):65- if(not existCode(bankCode, bic)):66- try:67- cursor.execute("INSERT INTO institutions (bankcode, bic, name) VALUES(?,?,?)", (bankCode, bic, bankName))68- except sqlite3.Error as e:69- print("Error: {0} while inserting {1} ({2})".format(e.args[0], bankCode, bic))70+ to_insert.append((bankcode, bic, name))7172- institutesFile = codecs.open(fileName, "r", encoding=args.encoding)73- for institute in institutesFile:74- bic = institute[284:295].strip()75- if len(bic) > 0:76- bcNumber = "{:0>5}".format(institute[2:7].strip() if institute[11:16] == " " else institute[11:16].strip())77- name = "%s (%s)" % (institute[54:114].strip(), institute[194:229].strip())78- submitInstitute(bcNumber, name, bic)79- rowsInserted += 180+ cursor = db.cursor()81+ cursor.execute("BEGIN")82+ try:83+ cursor.executemany(84+ "INSERT OR IGNORE INTO institutions (bankcode, bic, name) VALUES (?, ?, ?)",85+ to_insert86+ )87+ db.commit()88+ return cursor.rowcount89+ except sqlite3.Error as e:90+ db.rollback()91+ print(f"Database error: {e}")92+ return 09394- db.commit()95- return rowsInserted96-97-98if __name__ == '__main__':99parser = argparse.ArgumentParser(description="Creates a SQLite database for KMyMoney with information about IBAN and BICs based on a swiss BC-Bankenstamm file."100- " You can get the BC-Bankenstamm file from https://www.six-group.com/interbank-clearing/de/home/bank-master-data/download-bc-bank-master.html"101+ " You can get the BC-Bankenstamm file from https://api.six-group.com/api/epcd/bankmaster/v2/public/downloads/bcbankenstamm"102)103104parser.add_argument(dest='file', help='File to load')105parser.add_argument('-o', '--output', default="bankdata.ch.db", help='SQLite database to open/generate')106- parser.add_argument('-e', '--encoding', default="iso 8859-15", help='Charset of file')107args = parser.parse_args()108109- print("Read data from \"{0}\" with \"{1}\" encoding".format(args.file, args.encoding))110+ print(f'Read data from "{args.file}"')111db = sqlite3.connect(args.output)112113- createTable()114- institutions = processFile(args.file)115- print("Inserted {0} institutions into database \"{1}\"".format(institutions, args.output))116+ create_table()117+ institutions = process_file(args.file)118+ print(f'Inserted {institutions} institutions into database "{args.output}"')119120cursor = db.cursor()121cursor.execute("ANALYZE institutions")122123124