mt940.parser module

Format

Sources:

[] = optional
! = fixed length
a = Text
x = Alphanumeric, seems more like text actually. Can include special
    characters (slashes) and whitespace as well as letters and numbers
d = Numeric separated by decimal (usually comma)
c = Code list value
n = Numeric
mt940.parser.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.parser.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]