From 1cf5b3f63b197cd9f9a0a5d4289ee88cff23e269 Mon Sep 17 00:00:00 2001 From: Pete Houghton Date: Mon, 12 Aug 2024 16:36:40 +0000 Subject: [PATCH] initial stages of camt tools --- .gitignore | 2 + requirements.txt | 2 + tests/parse_camt053_msgs_test.py | 2 +- tools/camt053_to_excel.py | 41 +++++++++++++++++ tools/lookup.py | 78 ++++++++++++++++++++++++++++++++ 5 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 tools/camt053_to_excel.py create mode 100644 tools/lookup.py diff --git a/.gitignore b/.gitignore index 3498c31..ba9afb2 100644 --- a/.gitignore +++ b/.gitignore @@ -141,3 +141,5 @@ pain/ pyiso20022/ temp/ + +*.xlsx \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 753bb1a..221ff5e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,4 @@ pytest xsdata[cli,lxml,soap] +pandas +openpyxl diff --git a/tests/parse_camt053_msgs_test.py b/tests/parse_camt053_msgs_test.py index 7f782a6..33350ba 100644 --- a/tests/parse_camt053_msgs_test.py +++ b/tests/parse_camt053_msgs_test.py @@ -6,7 +6,7 @@ @pytest.mark.parametrize("expected_acc_id", [ ("DD01100056869") ]) -def test_parse_camt052_001_02(expected_acc_id): +def test_parse_camt053_001_02(expected_acc_id): parser = XmlParser() diff --git a/tools/camt053_to_excel.py b/tools/camt053_to_excel.py new file mode 100644 index 0000000..dbc73b5 --- /dev/null +++ b/tools/camt053_to_excel.py @@ -0,0 +1,41 @@ +from lxml import etree +import pandas as pd + + +def modify_key(key, translate=True): + return key.split('}')[-1] + + +def parse_element(element, parent_name=''): + data_dict = {} + for child in element: + child_name = f"{parent_name}_{child.tag}" if parent_name else child.tag + + if len(child): + data_dict.update(parse_element(child, child_name)) + else: + data_dict[child_name] = child.text + + modified_dict = {modify_key(k): v for k, v in data_dict.items()} + + return modified_dict + + +def camt053_to_excel(xml_fname, excel_fname): + with open(xml_fname, "rb") as xml_file: + xml_data = xml_file.read() + + root = etree.fromstring(xml_data) + data = [] + elements = root.xpath('//*[local-name()="Ntry"]') + + for record in elements: + record_data = parse_element(record) + data.append(record_data) + + df = pd.DataFrame(data) + + df.to_excel(excel_fname, index=False) + + +camt053_to_excel("example_files/gs_camt/camt053_001_02.xml", "camt053_001_02.xlsx") diff --git a/tools/lookup.py b/tools/lookup.py new file mode 100644 index 0000000..3e40bb2 --- /dev/null +++ b/tools/lookup.py @@ -0,0 +1,78 @@ +mnemonic_lookup = { + 'Bk': 'Bank', + 'Br': 'Bearer', + 'Cd': 'Code', + 'Dt': 'Date', + 'FI': 'FinancialInstitution', + 'Fr': 'From', + 'Id': 'Identification', + 'Nb': 'Number', + 'Nm': 'Name', + 'Of': 'Of', + 'Tm': 'Time', + 'To': 'To', + 'Tp': 'Type', + 'Tx': 'Transaction', + 'Adr': 'Address', + 'Agt': 'Agent', + 'Amt': 'Amount', + 'Biz': 'Business', + 'Ccy': 'Currency', + 'Cdt': 'Credit', + 'Clr': 'Clearing', + 'Cre': 'Creation', + 'Def': 'Definition', + 'End': 'End', + 'Fin': 'Financial', + 'For': 'For', + 'Grp': 'Group', + 'Hdr': 'Header', + 'Idr': 'Identifier', + 'Inf': 'Information', + 'Msg': 'Message', + 'Mtd': 'Method', + 'Nxt': 'Next', + 'Org': 'Organisation', + 'Pmt': 'Payment', + 'Pst': 'Post', + 'Pty': 'Party', + 'Rmt': 'Remittance', + 'Sum': 'Sum', + 'Svc': 'Service', + 'Sys': 'System', + 'Trf': 'Transfer', + 'Twn': 'Town', + 'Txs': 'Transactions', + 'Acct': 'Account', + 'Bldg': 'Building', + 'Cdtr': 'Creditor', + 'Chrg': 'Charge', + 'Ctgy': 'Category', + 'Ctrl': 'Control', + 'Ctry': 'Country', + 'Dbtr': 'Debtor', + 'Intr': 'Inter', + 'Othr': 'Other', + 'Prty': 'Priority', + 'Pstl': 'Postal', + 'Purp': 'Purpose', + 'Reqd': 'Requested', + 'Rltd': 'Related', + 'Root': 'Root', + 'Strt': 'Street', + 'UETR': 'UETR', + 'BICFI': 'BICFI', + 'Chrgs': 'Charges', + 'Cstmr': 'Customer', + 'Exctn': 'Execution', + 'Initg': 'Initiating', + 'Initn': 'Initiation', + 'Instd': 'Instructed', + 'Instg': 'Instructing', + 'Instn': 'Institution', + 'Instr': 'Instruction', + 'Prtry': 'Proprietary', + 'Sttlm': 'Settlement', + 'Ultmt': 'Ultimate', + 'Ustrd': 'Unstructured' +} \ No newline at end of file