cancel
Showing results for 
Search instead for 
Did you mean: 

convert cobol binary file to text file using python script

Pkrishna9494
New Contributor

I'm using below script  cobol binary file how to call it in script snap

Input :צנקננפקנעצףסץס q4Œ @ @@@ ננץ@@@@@@@@@@@@@@@@@@@@@@@@@@@2 <נננננ@@@@@@@@@@@@@@@ @@@@@@@@@@@@@@@@0 e2<  @@@@סננסעע

Out put:

" 00000000000000000000000000000000000607.","0047077581"," 00000000000000000000000000000000000001."," 0000000000000000000000072075.8900000000","0","0"," 0000000000000000000000000000.0000000000",""," 000.","","1240207","0"," 0000000000000000000000000000.0000000000"," 00000000000000000000000000000000000005.",""," 00000000000000000000000000000000032023."," 00000000000000000000000000000000000000."," 00000000000000000000000000000000000000.",""," 00000000000000000000000000000000000000.",""," 00000000000000000000000000000001240101."," 0000000000000000000000000625.6600000000"," 00000000000000000000000000000000000052."," 00000000000000000000000000000000000037.","","10","01","22",""

-----------------------------------------------------------------------------------------------------------------------------------

 

import struct

feild_widths = [10, 20, 30]

record_size = sum(field_widths)

with open('cobol_binary_file.dat', 'rb') as binary_file:
with open('output.txt', 'w') as text_file:
while True:
record_data = binary_file.read(record_size)
if not record_data:
break #End of file
#This converts binary data into feild
fields = [record_data[i:i+width].decode('utf-8').strip() for i, width in enumerate(field_widths)]
#Here it converts binary data to text file and writes it to output file.
text_data = ','.join(fields)
text_file.write(text_data + '\n')
print("Conversion complete. Output written to output.txt")

-

Thank you

1 REPLY 1

GanguP
New Contributor III

Does this work with packed fields

 

Thanks,

GP