This is how I read an EBCDIC file. in the read command you need to provide the length of the record. In this case it was 87. Might include a CR or LF as I copied the file to USS before reading.
fieldwidths = (8, 8, 4, -1, 1, -2, 7, 46, 8, 1, -2) # negative widths represent ignored padding fields
fmtstring = ' '.join('{}{}'.format(abs(fw), 'x' if fw < 0 else 's')
for fw in fieldwidths)
with open('file.txt', 'r', encoding="cp1140") as f: #source file encoding
read_data = bytes(f.read(87), 'utf-8') #destination encoding
while read_data:
fieldstruct = struct.Struct(fmtstring)
jobnames = namedtuple('F1','F2 F3 F4 F5 F6 F7 F8 F9')
parse = fieldstruct.unpack_from
fields = parse(read_data)
f1 = jobnames._make(fields)
I also used Struct to parse the fields.
This only works for character data. No packed decimal or binary fields.
Chris Hanks
------------------------------
Christopher Hanks
Architect
Citigroup Technology Inc
Tampa FL US
------------------------------
Original Message:
Sent: 12-16-2021 13:23
From: Gabor Markon
Subject: Python: read an EBCDIC file
I try to read a sequential dataset (FB LRECL(1000)) by using this simple code:
# ==================================================
count = 0
thefilepath = "//'sa8sb22.resultfb'"
count = len(open(thefilepath, 'r', encoding="cp500").readlines( ))
print("There are ", count , " lines in the file")
# ==================================================
I have 133 records in the file, but the above code delivers 1 (one). I suppose, no read is executed.
I suppose it is realted to the encoding, but I don't find any better as the cp500.
The file is definitive EBCDIC (x'81' for 'a').
Any hints pls.?
------------------------------
Gabor Markon
Mainframe Architect
Self Registered
Budapest HU
------------------------------