[Python] Reading an IBM PIC S9(09) COMP data from file in python

Discussão em 'Python' iniciado por Stack, Agosto 6, 2025.

  1. Stack

    Stack Membro Participativo

    I was given sample data by the mainframe engineer, for which the COPYBOOK looks like below:

    01 CCS001-DETAIL-RECORD.
    05 CCS001-REC-TYPE PIC X(01). Should be '1'
    05 CCS001-MEMB-NUM PIC S9(09) COMP.
    05 CCS001-PR-CAT PIC X(03).
    05 CCS001-CM-TS PIC X(26).
    05 CCS001-CR-ID PIC X(08).
    05 FILLER PIC X(38).


    When I read it in python and printed it, it looked like below:

    b'1\x07\xc2\xae\xc2\x82\xc3\x9dABC...'
    b'1\x07\xc2\xaen\xc3\xb4ABC...'
    b'1\x07^\x15jABC...'
    b'1\x07^\xc3\xa4\xc3\xbbABC...'
    b'1\x07^\xc2\xb8\xc2\xb7ABC...'
    b'1\x07^z\x10ABC...'
    b'1\x07^\xc2\xae\xc3\x81ABC...'
    b'1\x07^X\xc3\xb6ABC...'
    b'1\x07^\xc3\x9b\xc3\x94ABC...'
    b'1\x07\xc2\xa3\xc3\xa1`ABC...'
    b'1\x07\xc2\xb7%vABC...'


    where ...' includes more data, but I am not interested in those. I am interested in the CCS001-MEMB-NUM field. The ABC values are for the field CCS001-PR-CAT. The python code I used is below:

    with open('sample.dat', 'rb') as f:
    data = f.read()
    records=data.split(b'\r\n')
    for i, record in enumerate(records):
    if not record:
    continue
    print(record)
    if i==10:
    break


    When I opened the file in PyCharm, it looks like below:

    [​IMG]

    However, if I were to assume that the COMP field should be 4 bytes, then the output in Bytes shows that they are not of the equal length, i.e. ABC does not start at the same position on every line. Does it mean that the data which is copy pasted by the engineer into a text file got corrupted? Or am I using the wrong logic to understand?

    Continue reading...

Compartilhe esta Página