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:
JSONEncoderSerialize MT940 model objects to JSON-compatible primitives.
Dates, datetimes, timedeltas, timezones and decimals are rendered as strings;
Transactions,Transaction,BalanceandAmountare rendered as theirdata/__dict__mappings. Pass it as theclsargument tojson.dumps().- default(o)[source]¶
Return a JSON-serializable representation of
o.- Parameters:
o (Any) – The object to serialize.
okeeps the permissiveAnytype of the overriddenjson.JSONEncoder.default().- Returns:
The serialized form of the object.
- Return type:
- 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:
- 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 singleTransactions, this splits the input on:20:statement boundaries and parses each block into its ownTransactions. Use it for files that concatenate several statements (e.g. balance-only blocks), where a singleTransactionswould 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 withtransaction_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
Transactionsper statement block.- Return type:
Submodules¶
- mt940.json module
- mt940.models module
- mt940.parser module
- mt940.processors module
- mt940.tags module
- Format
TagDateTimeIndicationTransactionReferenceNumberRelatedReferenceAccountIdentificationStatementNumberFloorLimitIndicatorNonSwiftBalanceBaseOpeningBalanceFinalOpeningBalanceIntermediateOpeningBalanceStatementStatementASNBStatementGLSClosingBalanceIntermediateClosingBalanceFinalClosingBalanceAvailableBalanceForwardAvailableBalanceTransactionDetailsSumEntriesSumDebitEntriesSumCreditEntriesTagsTags.DATE_TIME_INDICATIONTags.TRANSACTION_REFERENCE_NUMBERTags.RELATED_REFERENCETags.ACCOUNT_IDENTIFICATIONTags.STATEMENT_NUMBERTags.OPENING_BALANCETags.INTERMEDIATE_OPENING_BALANCETags.FINAL_OPENING_BALANCETags.STATEMENTTags.CLOSING_BALANCETags.INTERMEDIATE_CLOSING_BALANCETags.FINAL_CLOSING_BALANCETags.AVAILABLE_BALANCETags.FORWARD_AVAILABLE_BALANCETags.TRANSACTION_DETAILSTags.FLOOR_LIMIT_INDICATORTags.NON_SWIFTTags.SUM_ENTRIESTags.SUM_DEBIT_ENTRIESTags.SUM_CREDIT_ENTRIES
TAG_BY_ID
- mt940.utils module