Read & Convert COBOL Mainframe Extracts (coboller)#
The dataspector.coboller package (and its coboller CLI) lets you
read COBOL copybooks and mainframe data extracts in pure Python — turning
opaque EBCDIC, fixed-width, and compressed binary mainframe files (including
COMP-3 packed decimals, COMP-6, binary COMP/COMP-5/COMP-X
integers, COMP-1/COMP-2 IEEE & IBM HFP floats, signed zoned decimals,
OCCURS arrays, and REDEFINES overlays) into clean
pandas.DataFrame tables and human-readable CSV, TSV, JSON/JSONL,
Excel, or Parquet files with zero JVM or external compiler dependencies.
Tip
See the pre-executed Jupyter benchmark notebook notebooks/validate_mainframe_extracts.ipynb for an end-to-end validation of 69 COBOL copybooks and 37 real-world mainframe data extracts (5,863 records) across public enterprise mainframe benchmark suites (100% pass rate).
Interactive Exploration & Conversion Walkthrough#
Raw mainframe extracts mix EBCDIC characters with binary packed decimals and floating-point bytes that are unreadable in standard editors:
>>> import pathlib
>>> pathlib.Path("tests/data/sample_cobol_mainframe.dat").read_bytes()[:40]
b'\xd5\xc1\xf1\xf0\xf0\xf0\xf0\xf0\xf1\xf7@@\xc1\xf2\xf0\xf2\xf4\xf0\xf1\xf1\xf5\xf0\xf0\xf1\xf2\xf5\xf5\xc0\x00$\x00\x0b^(=.\x14{?\xf0'
With Copybook, you can inspect the layout,
preview decoded rows, convert to modern formats, and validate against an
auto-generated Pandera schema in just a few lines:
>>> from dataspector.coboller import Copybook
>>> cbl = Copybook("tests/data/sample_cobol_mainframe.cbl")
>>> cbl.record_length
67
# 1. Inspect field names, PIC clauses, storage USAGE, byte offsets, and widths
>>> cbl.summary(include_filler=False)
name level pic usage kind offset byte_length signed decimals is_redefines
0 ACCT-REGION 10 XX DISPLAY ALPHANUMERIC 0 2 False 0 False
1 ACCT-NUMBER 10 X(7) DISPLAY ALPHANUMERIC 2 7 False 0 False
2 ACCT-CHECK-DIGIT 10 X DISPLAY ALPHANUMERIC 9 1 False 0 False
3 ACCT-STATUS 5 X DISPLAY ALPHANUMERIC 12 1 False 0 False
4 ACCT-EFF-DATE 5 9(8) DISPLAY INTEGER 13 8 False 0 False
5 ACCT-ADJUSTMENT 5 S9(5)V99 DISPLAY DECIMAL 21 7 True 2 False
6 ACCT-TERM-MONTHS 5 S9(4) COMP BINARY 28 2 True 0 False
7 ACCT-SCORE 5 S9(9) COMP BINARY 30 4 True 0 False
8 ACCT-RATE 5 COMP-1 FLOAT 34 4 True 0 False
9 ACCT-FACTOR 5 COMP-2 FLOAT 38 8 True 0 False
10 ACCT-BALANCE 5 S9(9)V99 COMP-3 PACKED_DECIMAL 46 6 True 2 False
11 PERIOD-AMT 10 S9(7)V99 COMP-3 PACKED_DECIMAL 52 5 True 2 False
12 PERIOD-AMT-01 10 S9(7)V99 COMP-3 PACKED_DECIMAL 57 5 True 2 False
13 PERIOD-AMT-02 10 S9(7)V99 COMP-3 PACKED_DECIMAL 62 5 True 2 False
# 2. Decode into a pandas DataFrame (use nrows=... to preview huge files instantly)
>>> df = cbl.to_dataframe(
... "tests/data/sample_cobol_mainframe.dat",
... encoding="cp500",
... include_filler=False,
... nrows=5,
... )
>>> df[["ACCT-REGION", "ACCT-NUMBER", "ACCT-STATUS", "ACCT-EFF-DATE", "ACCT-ADJUSTMENT", "ACCT-BALANCE"]]
ACCT-REGION ACCT-NUMBER ACCT-STATUS ACCT-EFF-DATE ACCT-ADJUSTMENT ACCT-BALANCE
0 NA 1000001 A 20240115 125.50 250000.00
1 EU 1000002 A 20240220 -42.75 -15000.50
2 AP 1000003 C 20231201 0.00 0.00
3 NA 1000004 A 20240630 9999.99 1234567.89
# 3. Convert fixed-width & compressed COBOL files into human-readable formats
>>> cbl.convert("tests/data/sample_cobol_mainframe.dat", "accounts.csv", encoding="cp500")
PosixPath('accounts.csv')
>>> cbl.convert("tests/data/sample_cobol_mainframe.dat", "accounts.jsonl", encoding="cp500")
PosixPath('accounts.jsonl')
>>> cbl.convert("tests/data/sample_cobol_mainframe.dat", "accounts.xlsx", encoding="cp500")
PosixPath('accounts.xlsx')
Note
convert() streams chunk-by-chunk for
.csv, .tsv, and .jsonl outputs, allowing multi-gigabyte
mainframe files to be converted with constant memory usage.
Command-Line Inspection & Conversion (coboller)#
DataSpector ships a dedicated coboller CLI command for quick terminal
inspection and format conversion:
# Print the copybook's field layout and byte offsets
uv run coboller inspect tests/data/sample_cobol_mainframe.cbl --omit_filler true
# Preview decoded rows on stdout (default: 10 rows)
uv run coboller convert tests/data/sample_cobol_mainframe.cbl tests/data/sample_cobol_mainframe.dat \
--encoding cp500 --nrows 5
# Convert EBCDIC + COMP-3 binary file to CSV, TSV, JSONL, JSON, Excel, or Parquet
uv run coboller convert tests/data/sample_cobol_mainframe.cbl tests/data/sample_cobol_mainframe.dat \
--encoding cp500 --include_redefines true --output accounts.csv
Supported COBOL Data Types#
COBOL Clause |
Storage / Usage |
Physical Width |
Python Output Type |
|---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
signed |
|
|
|
signed |
|
|
expanded length |
|
|
|
expanded length |
|
|
|
|
|
|
|
|
|
|
|
2 / 4 / 8 bytes |
|
|
|
2 / 4 / 8 bytes |
|
|
|
|
|
|
COBOL-2002 C-interop binary |
1 / 2 / 4 / 8 bytes |
|
|
32-bit float (IEEE / IBM HFP) |
4 bytes |
|
|
64-bit float (IEEE / IBM HFP) |
8 bytes |
|
|
Unsigned address |
4 or 8 bytes |
|
|
Signed table index |
4 bytes |
|
Structural COBOL Features#
Fixed-format & free-format source: strips sequence numbers (columns 1–6), identification fields (columns 73–80), column-7 comments (
*,/), and free-format inline comments (*>).Hierarchical groups & group-level ``USAGE``: child fields inherit group
USAGE(e.g.05 BIN-GROUP USAGE BINARY.) unless overridden.``OCCURS n [TIMES]`` (and
OCCURS m TO n DEPENDING ON): expands nested arrays on both fields and groups with zero-padded suffixes (-01,-02, or 3-digit-001whenoccurs >= 100).``REDEFINES``: tracks overlays on the redefined target without inflating primary record length; pass
include_redefines=Trueto decode overlays at the exact same byte offsets alongside the primary fields.Level ``88`` condition names: attached to their parent
ElementaryItemasConditionNameobjects rather than polluting physical columns.``FILLER`` & unnamed fields: included in byte-offset calculations and optionally omitted from output DataFrames via
include_filler=False.
Streaming Large Mainframe Files#
For large files that do not fit in memory, iterate chunk-by-chunk with
yield_data():
from dataspector.coboller import CopyBook
cbl = CopyBook("tests/data/sample_cobol_mainframe.cbl")
for chunk in cbl.yield_data(
"tests/data/sample_cobol_mainframe.dat",
chunksize=500,
encoding="cp500", # EBCDIC International (or 'cp037', 'ascii', 'utf-8')
float_format="ieee", # or 'ibm' for IBM System/390 HFP COMP-1/COMP-2
byteorder="big", # IBM mainframe default ('little' for x86 COBOL)
as_decimal=False, # True for exact decimal.Decimal fixed-point values
include_redefines=True, # Also decode REDEFINES overlays
include_filler=False, # Omit FILLER columns from DataFrame
):
process(chunk)
Automatic Pandera Schema Generation#
Generate a pandera.pandas.DataFrameSchema directly from any copybook
to validate column types, string widths, numeric digit bounds, and level-88
allowed values:
schema = cbl.to_pandera_schema(include_filler=False, enforce_conditions=True)
validated_df = schema.validate(df)
Or combine COBOL parsing directly with run_checks_on_location():
import dataspector
history = dataspector.run_checks_on_location(
"cobol://tests/data/sample_cobol_mainframe.cbl::tests/data/sample_cobol_mainframe.dat?encoding=cp500&include_filler=false",
[
("source_exists", {}),
("record_count", {"nrows": 4}),
("record_field_numeric_value_between", {"field": "ACCT-TERM-MONTHS", "constraint": (1, 360)}),
("record_validate_schema", {"schema": "tests/data/sample_cobol_mainframe.cbl"}),
],
)
Low-Level Packing & Unpacking Helpers#
coboller also exports standalone, round-trippable binary helpers for custom
pipelines and test data generation:
Real-World Mainframe Extract Benchmark & Performance#
DataSpector includes an executable Jupyter validation notebook —
validate_mainframe_extracts.ipynb
(also viewable pre-rendered on
GitHub)
— that automatically downloads and validates 69 COBOL copybooks and 37
mainframe data extracts (5,863 records, 1.03 MB) across three benchmark suites
with a 100% pass rate:
Benchmark Suite |
Copybooks |
Extracts |
Records |
Key Features Validated |
|---|---|---|---|---|
Enterprise Mainframe Modernization Suite (AWS CardDemo) |
30 / 30 (100%) |
16 / 16 (100%) |
1,343 |
z/OS EBCDIC |
Enterprise Financial & Banking Torture Suite |
36 / 36 (100%) |
18 / 18 (100%) |
4,480 |
195-field type torture matrix ( |
DataSpector Built-in Suite ( |
3 / 3 (100%) |
3 / 3 (100%) |
40 |
|
Total |
69 / 69 (100%) |
37 / 37 (100%) |
5,863 |
Zero decoding failures across all 37 extracts |
Throughput & Latency (Pure Python, Zero JVM)#
Copybook parsing: all 69 copybooks (3,465 elementary field definitions) parse into flat byte-offset layouts in ~26 ms total (~0.38 ms / copybook, >2,500 copybooks/sec).
Binary EBCDIC + COMP-3 decoding: decodes 100,000 binary EBCDIC + COMP-3 transaction records (4.5 MB, 600,000 field values) into a
pandas.DataFramein ~0.6s (~150,000+ records/sec, ~1,000,000 decoded fields/sec), with constant-memory chunked streaming viayield_data()andconvert().