From fcb6e337d3925eb8ddb25370fbc2adfb69113ab4 Mon Sep 17 00:00:00 2001 From: Pete Houghton Date: Wed, 14 Aug 2024 14:15:20 +0000 Subject: [PATCH] Added test and fixed leaf naming --- pyiso20022/tools/camt053_extract.py | 21 +++++++++++++++------ tests/tools_camt053_msgs_test.py | 4 ++-- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/pyiso20022/tools/camt053_extract.py b/pyiso20022/tools/camt053_extract.py index 8b3d142..93c1ca2 100644 --- a/pyiso20022/tools/camt053_extract.py +++ b/pyiso20022/tools/camt053_extract.py @@ -2853,16 +2853,17 @@ def _iso20022_term_translator(mnemonic): new_name = "" - list_of_mnems = re.split(r'(?<=[a-z])(?=[A-Z])', mnemonic) + list_of_mnems = re.split(r'(?<=[a-z\/ ])(?=[ \/A-Z])', mnemonic) for key in list_of_mnems: new_name += mnemonics.get(key, key) new_name += " " - return new_name.strip() + squashed = ' '.join(new_name.split()) + return squashed.strip() -def _modify_key(key, translate=True): - clean_mnems = key.split('}')[-1] + +def _modify_key(clean_mnems, translate=True): if translate: clean_mnems = _iso20022_term_translator(clean_mnems) return clean_mnems @@ -2871,7 +2872,7 @@ def _modify_key(key, translate=True): def _parse_element(element, parent_name='', translate=True): data_dict = {} for child in element: - child_name = f"{parent_name}_{child.tag}" if parent_name else child.tag + child_name = f"{parent_name} / {child.tag}" if parent_name else child.tag if len(child): data_dict.update(_parse_element(child, @@ -2894,7 +2895,15 @@ def camt053_to_df(xml_fname, translate=True): root = etree.fromstring(xml_data) data = [] - elements = root.xpath('//*[local-name()="Ntry"]') + + for elem in root.getiterator(): + if not hasattr(elem.tag, 'find'): + continue + ind = elem.tag.find('}') + if ind > 0: + elem.tag = elem.tag[ind+1:] + + elements = root.xpath('//Ntry') for record in elements: record_data = _parse_element(record, translate=translate) diff --git a/tests/tools_camt053_msgs_test.py b/tests/tools_camt053_msgs_test.py index 975a7a5..4734ecd 100644 --- a/tests/tools_camt053_msgs_test.py +++ b/tests/tools_camt053_msgs_test.py @@ -11,7 +11,7 @@ def test_pandas_df_camt053_001_02(entry_reference, msg_id): df = camt053_to_df("example_files/gs_camt/camt053_001_02.xml") - result = df[df['Entry Reference'] == entry_reference]["Message Identification"] + result = df[df['Entry Reference'] == entry_reference]["Entry Details / Transaction Details / References / Message Identification"] assert result.values[0] == msg_id @@ -30,5 +30,5 @@ def test_excel_camt053_001_02(entry_reference, msg_id): tmp_file.close() - result = df[df['Entry Reference'].astype(str) == str(entry_reference)]["Message Identification"] + result = df[df['Entry Reference'].astype(str) == str(entry_reference)]["Entry Details / Transaction Details / References / Message Identification"] assert result.values[0] == msg_id