Skip to content

MT942 support

What the loader reads, how each :61: line becomes a Transaction, how amounts are signed, and what the summary holds. This page is the README's Features section, included verbatim so the two cannot drift.

Supported tags

Tag Meaning Cardinality
:20: Transaction reference number mandatory
:25: Account identification (the account id) mandatory
:28C: Statement / sequence number optional
:34F: Floor limit indicator <CCY>[D\|C]<amount> (provides the currency) one or two
:13D: Date/time stamp YYMMDDHHMM±HHMM optional
:61: Statement line (one per booked entry) repeatable
:86: Information to account owner (attaches to the preceding :61:) optional, per line
:90D: Debit summary <count><CCY><amount> optional
:90C: Credit summary <count><CCY><amount> optional

Unrecognised tags (and the trailing - end-of-message marker) are silently ignored, so future SWIFT additions do not break parsing. A :61: line that does not match the statement-line grammar is skipped rather than fatal, so one unrecognised row never aborts the whole parse.

Real-world wire-format constructs

Genuine bank/SWIFT exports are messier than the tidy sample above. The loader handles the following, pinned by a golden test over a real, third-party fixture in tests/fixtures/real/ (its source, licensing and provenance are in tests/fixtures/real/PROVENANCE.md):

  • SWIFT message envelope. Messages wrapped in the header blocks {1:...}{2:...}{3:...} and the text block {4: ... -} are unwrapped before parsing; the envelope is never mistaken for content.
  • Amounts with no fractional digits. A SWIFT amount may end on the decimal comma with nothing after it: 5000, → Decimal("5000"), 0, → Decimal("0").
  • :34F: with an embedded D/C indicator. :34F:NZDC0, yields currency NZD regardless of the C/D letter.
  • Multi-line :86:. Continuation lines that do not start with a :tag: head (e.g. /BAI/…, /BENM/…, /ACNO/…) are appended to the :86: description, newlines preserved; CRLF and LF files give the same description.
  • Supplementary details after :61:. A line immediately following a :61: (before any :86:), such as a bare Transfer or wording/NBKT, is the SWIFT supplementary-details subfield. It is folded into the transaction's description (supplementary text first, then the :86: content, joined by newlines) and never glued onto the statement-line tail, so the parsed transaction_id / reference stay clean. A :61: with supplementary details but no following :86: keeps that text as its description.

Field mapping

Each :61: statement line becomes one Transaction:

Transaction field Source
account_id :25:
currency :34F:
amount :61: amount as Decimal, negated for D, RC (reversal of credit) and ED (expected debit) lines; C, RD and EC stay positive
value_date :61: value date (YYMMDD)
booking_date :61: optional entry date (MMDD, inherits the value-date year)
transaction_id the bank reference in the :61: tail (left of //)
reference the customer reference in the :61: tail (right of //)
description the following :86: free-form text
source always "mt942"
source_index the zero-based line index within the message

The two-digit YY year uses the standard SWIFT sliding window: 00-79 map to 20YY, 80-99 to 19YY. An optional one-letter funds code after the debit/credit mark is accepted and not stored.

Summaries

When you want the message metadata and the :90D:/:90C: roll-ups rather than the individual transactions, call summarize_mt942:

from bankstatementparser_loader_mt942 import summarize_mt942

summary = summarize_mt942(mt942)
print(summary.reference)          # MT942REF001
print(summary.currency)           # EUR
print(summary.debit_count)        # 1
print(summary.debit_sum)          # Decimal("200.50")
print(summary.credit_sum)         # Decimal("500.00")
print(summary.transaction_count)  # 2

Mt942Summary is a frozen dataclass with reference, account_id, currency, statement_datetime, debit_count, debit_sum, credit_count, credit_sum, and transaction_count. Optional fields (no :90x: / :13D: present) default to None.

Errors

A payload missing the mandatory :20: reference or :25: account identification raises ValueError with a message naming the missing tag:

load_mt942(":25:ACC\n:61:250624C10,00NTRFX\n")
# ValueError: MT942 payload missing required :20: reference

A malformed :90D: / :90C: summary, an impossible date, or an amount that is not a SWIFT 15d comma-decimal (digits with at most one comma, at most 15 characters) also raises ValueError.