Data Automation & Cleaning
Intermediate
Privacy-First In-Memory Bank Statement PII Masker (Zero-Disk Retention) in Python
Process uploaded bank statement CSV/PDF streams strictly in RAM using Python `io.BytesIO` and mask account numbers, IFSC, mobile numbers, and UPI IDs via Regex.
#pandas
Problem Statement
Fintech diagnostic apps and credit analyzers must process sensitive bank statements in RAM (`io.BytesIO`) without saving files to disk, while redacting PII (account numbers, mobile numbers, UPI handles, and PANs) before passing data to analytics engines.
Formula / Core Logic
Reads raw bytes via io.BytesIO(raw_bytes), applies compiled regex masking for UPI (@okaxis, @ybl), 10-digit mobile numbers, and account numbers, and returns sanitized DataFrame in memory.
Installation
pip install pandas
in-memory-zero-disk-bank-statement-privacy-sanitizer-python.py
import io
import re
import pandas as pd
PII_PATTERNS = [
(r'[a-zA-Z0-9.\-_]{2,256}@[a-zA-Z]{2,64}', '[MASKED_UPI]'),
(r'\b[A-Z]{5}[0-9]{4}[A-Z]{1}\b', '[MASKED_PAN]'),
(r'\b[6-9][0-9]{9}\b', '[MASKED_MOBILE]'),
(r'\b[0-9]{9,18}\b', '[MASKED_ACCT_NO]')
]
def process_statement_in_memory(file_bytes: bytes, desc_col='Narration') -> pd.DataFrame:
buffer = io.BytesIO(file_bytes)
df = pd.read_csv(buffer)
def redact_text(val):
s = str(val)
for pattern, replacement in PII_PATTERNS:
s = re.sub(pattern, replacement, s)
return s
if desc_col in df.columns:
df[desc_col] = df[desc_col].apply(redact_text)
buffer.close() # Zero disk retention
return df
Get the Complete 100+ Python Quant & Automation Bundle
Instant lifetime access to all production-ready scripts, valuation models, and trading bots ($19).
Buy Full Bundle on Gumroad ($19) →Frequently Asked Questions
Q: Why use `io.BytesIO` instead of ` tempfile.NamedTemporaryFile`?
`io.BytesIO` keeps the entire file stream inside volatile RAM, guaranteeing that zero sensitive financial records ever touch the server's SSD/disk.