0

I have a binary file from a mainframe which I'm trying to read using Python and produce a human readable text file. I'm still gathering more information about the file. What I do know is that the file serves as input to COBOL programs. I try to read the file into python like this:

with open('P_MF.DAT', mode='rb') as f:
    file_content = f.read(500)

When I print(file_content) I get something like:

b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00@@@@@@@@\x00\x00@@@\xf0\xf0\xf0\xf0\xf0\xf0\xf0\xf0\xf0\xf0\xf0\xf0\xf0\xf0\xf0@@@@\x00\x00\x00\x00\x00\x00\x00\x00@@@\x00\x00\x00\x00@@@@@@\xf0\xf0\xf0\xf4\x00\x00\x00\x00\x08\x02\x00\x00Q\x08c\x18\x1f\xc5@@@\x00\x00\x000\x00\x00\x0f\x00\x00\x00\x01\x11?\x00\x00\x10\x02F\x17o@@\xd5@\xc9\xd5\xc7\xd3\xc9\xe2@\xd4\xc1\xd9\xe8@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\x00\x00\x00\x00\x00\x00\x00\x0c\x00\x00\x00\x00\x00\x00\x00'

Then I tried this using the codecs module which also gives me gibberish:

import codecs
file_content1 = codecs.decode(file_content, 'cp500')

But I can see a few readable characters in the output when I print(file_content1):

'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 \x00\x00 000000000000000 \x00\x00\x00\x00\x00\x00\x00\x00 \x00\x00\x00\x00 0004\x00\x00\x00\x00\x97\x02\x00\x00é\x97Ä\x18\x1fE \x00\x00\x00\x90\x00\x00\x0f\x00\x00\x00\x01\x11\x1a\x00\x00\x10\x02ã\x87? N INGLIS MARY \x00\x00\x00\x00\x00\x00\x00\x0c\x00\x00\x00\x00\x00\x00\x00'

I've been googling around for a couple of days. Tried a number of things like this - Python read a binary file and decode I feel like I'm getting nowhere with this problem. I also plan to ask how this file looks if read in a mainframe. I'd appreciate any info/help/advice at this point. ​

2
  • 2
    Decoding implies text. Not all binary data is text data. You have a mix of binary and text data and must know the structure of the data and only decode the text portions. Commented Mar 30, 2021 at 22:55
  • Thanks @MarkTolonen. That makes a lot of sense. Will get the structure of the data and try it. Commented Mar 31, 2021 at 15:42

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.