Forum Discussion

Pkrishna9494's avatar
Pkrishna9494
New Contributor
2 years ago

convert cobol binary file to text file using python script

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

2 Replies

  • GanguP's avatar
    GanguP
    New Contributor III

    Does this work with packed fields

     

    Thanks,

    GP

  •  

    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:

    Python Script to Process COBOL Binary File

    1. Import necessary modules:

      • struct for handling binary data.
      • os for file operations.
    2. Define field widths:

      • Create a list of field widths according to your COBOL file layout.
    3. Read and process the binary file:

      • Open the COBOL binary file in read-binary mode.
      • Open an output text file in write mode.
      • Read the binary data and decode it based on the defined field widths.
      • Write the decoded data to the output text 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")

    How to Run the Script

    1. Ensure Python is installed on your machine.
    2. Place your COBOL binary file (e.g., cobol_binary_file.dat) in the same directory as your script.
    3. Run the script from the command line or an IDE.

    Notes:

    • Field Widths: Make sure to adjust the field_widths list to match the actual widths of the fields in your COBOL file.
    • Character Encoding: The script assumes UTF-8 encoding. Adjust if your file uses a different encoding.
    • Error Handling: Add error handling as needed for robustness, especially if dealing with potentially corrupted files or unexpected data formats.

    Experts Opinion.