mt940 package

mt940 — parse MT940 bank statement files into rich Python objects.

The high-level entry point is parse(), which accepts a filename, a file handle, or raw str/bytes and returns a Transactions collection you can iterate over. Use parse_statements() for files that concatenate several statements, and JSONEncoder to serialize the result to JSON.

Example

>>> import mt940
>>> data = (
...     ':20:REF\n'
...     ':25:NL00BANK0123456789\n'
...     ':28C:1/1\n'
...     ':60F:C091019EUR1000,00\n'
...     ':61:0910201020C500,00NTRFNONREF//B\n'
...     ':86:Example transaction\n'
...     ':62F:C091020EUR1500,00\n'
... )
>>> transactions = mt940.parse(data)
>>> len(transactions)
1
>>> transactions[0].data['amount']
<500.00 EUR>
class mt940.JSONEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)[source]

Bases: JSONEncoder

Serialize MT940 model objects to JSON-compatible primitives.

Dates, datetimes, timedeltas, timezones and decimals are rendered as strings; Transactions, Transaction, Balance and Amount are rendered as their data/__dict__ mappings. Pass it as the cls argument to json.dumps().

default(o)[source]

Return a JSON-serializable representation of o.

Parameters:

o (Any) – The object to serialize. o keeps the permissive Any type of the overridden json.JSONEncoder.default().

Returns:

The serialized form of the object.

Return type:

Any

mt940.parse(src, encoding=None, processors=None, tags=None, transaction_boundary=None)[source]

Parse MT940 data into a single Transactions.

Parameters:
  • src (Source) – A file handle, a filename to read, or the raw data as str/bytes.

  • encoding (str | None) – Optional encoding override for byte input.

  • processors (Processors | None) – Optional extra pre/post processors.

  • tags (dict[int | str, mt940.tags.Tag] | None) – Optional extra or overriding tag parsers.

  • transaction_boundary (Iterable[str] | None) – Optional iterable of tag slugs that each start a new transaction (issue #110). By default only :61: starts a transaction; pass e.g. {'transaction_reference_number'} to also start one on every :20:. Omit it to keep the legacy behaviour.

Returns:

The parsed collection of transactions.

Return type:

Transactions

mt940.parse_statements(src, encoding=None, processors=None, tags=None, transaction_boundary=None)[source]

Parse an mt940 file that contains multiple statement blocks.

Unlike parse(), which merges everything into a single Transactions, this splits the input on :20: statement boundaries and parses each block into its own Transactions. Use it for files that concatenate several statements (e.g. balance-only blocks), where a single Transactions would only keep the last block’s statement-level data such as the opening/closing/available balances (issue #107).

Each :20: is treated as the start of a new statement, matching the standard where :20: is the once-per-statement transaction reference. This is therefore mutually exclusive with transaction_boundary={'transaction_reference_number'} (issue #110), which instead treats :20: as an intra-statement transaction boundary; the two target different, non-standard bank formats – don’t combine them.

Parameters:
  • src (Source) – A file handle, a filename to read, or the raw data as str/bytes.

  • encoding (str | None) – Optional encoding override for byte input.

  • processors (Processors | None) – Optional extra pre/post processors (applied per block).

  • tags (dict[int | str, mt940.tags.Tag] | None) – Optional extra or overriding tag parsers (applied per block).

  • transaction_boundary (Iterable[str] | None) – See parse() (and the note above).

Returns:

One Transactions per statement block.

Return type:

list[Transactions]

Submodules