MT940 Parser
mt-940 is a Python library designed to parse MT940 files, a standard format for bank account statements. It converts the complex MT940 data into easily manageable Python collections, enabling further statistics and manipulation. Currently at version 4.30.0, the library is actively maintained with frequent minor releases addressing various bank-specific parsing nuances and improvements.
Warnings
- gotcha MT940 format has many bank-specific variations. While `mt-940` aims for flexibility, highly customized or malformed files might not parse perfectly with default settings, potentially requiring custom pre- or post-processors.
- gotcha Encoding issues are common when parsing files. If the MT940 file is not UTF-8, you might encounter `UnicodeDecodeError` or garbled characters.
- gotcha Rich transaction details, especially within Tag 86, can be highly structured and bank-dependent. Default parsing may not fully break down these sub-fields into distinct data points.
- deprecated Although the library states support for Python 2.7, its string and unicode handling differs significantly from Python 3, making it a common source of encoding-related pitfalls and compatibility challenges.
Install
-
pip install mt-940
Imports
- mt940
import mt940
Quickstart
import mt940
import pprint
import io
# Example MT940 data as a string
mt940_data = """{1:F01BANKBEBBAXXX0000000000}{2:I940BANKBEBBAXXXN}{4:\n:20:TRN0001\n:25:BE00000000000000/EUR\n:28C:1/1\n:60F:C240315EUR1000,00\n:61:240316C100,00NTRFNONREF\n:86:Test transaction description for example.\n:62F:C240316EUR900,00\n-}"""
# Parse from a string (using io.StringIO to simulate a file)
transactions = mt940.parse(io.StringIO(mt940_data))
print("Parsed Transactions Data:")
pprint.pprint(transactions.data)
print("\nIndividual Transactions:")
for transaction in transactions:
print(f" Date: {transaction.data.get('date')}, Amount: {transaction.data.get('amount')}, Description: {transaction.data.get('entry_details')}")
# Accessing a specific field, e.g., the closing balance
closing_balance_info = transactions.data.get('closing_balance', {})
if closing_balance_info:
print(f"\nClosing balance: {closing_balance_info.get('amount')} {closing_balance_info.get('currency')}")
else:
print("\nClosing balance not found.")