02-19-2024 05:35 AM
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
04-16-2024 02:54 PM
Does this work with packed fields
Thanks,
GP
05-28-2024 08:56 AM
To call a COBOL binary file in a script and process it as you described, you can use a Python script to read the binary data, decode it, and write the output to a text file. Here’s an enhanced version of your script to handle the binary data more effectively:
Import necessary modules:
Define field widths:
Read and process the binary file:
Here is the complete script:
import struct
# Define field widths according to your COBOL file layout
field_widths = [10, 20, 30] # Update this list based on your actual field widths
record_size = sum(field_widths)
# Open the COBOL binary file in read-binary mode
with open('cobol_binary_file.dat', 'rb') as binary_file:
# Open the output text file in write mode
with open('output.txt', 'w') as text_file:
while True:
# Read a record of size record_size from the binary file
record_data = binary_file.read(record_size)
if not record_data:
break # End of file
# Decode the binary data into fields based on the field widths
fields = []
offset = 0
for width in field_widths:
field = record_data[offset:offset + width].decode('utf-8').strip()
fields.append(field)
offset += width
# Join the fields with commas and write to the output text file
text_data = ','.join(fields)
text_file.write(text_data + '\n')
print("Conversion complete. Output written to output.txt")
Experts Opinion.