0

We had DataInputStream for processing binary files in Java; what can we use for these files in Python?

tshepang
  • 12,111
  • 21
  • 91
  • 136
  • 2
    read this post: http://stackoverflow.com/questions/1035340/reading-binary-file-in-python and this one too: http://stackoverflow.com/questions/8710456/reading-a-binary-file-with-python – theAlse Mar 15 '13 at 09:05
  • Does you code need to be compatible with DataInputStream's file format? – Ber Mar 15 '13 at 09:24

3 Answers3

2

I have used the Construct package a lot to read and parse structures data in Python.

Basically it lets you declare the file's structure in a very idiomatic and pythonic way and than parses or encode it for you.

After parsing you have an object that allows access to all the file's information via attributes.

Ber
  • 40,356
  • 16
  • 72
  • 88
1

open("file", "b") opens the file and you can read it. See here.

Albert
  • 65,406
  • 61
  • 242
  • 386
1

Few years ago I used struct module to parse binary responses from several game servers http://docs.python.org/2/library/struct.html#struct.unpack

Sometimes it's usefull just to .find() some bytes in data, like .find('\x00') to go to the end of NULL-terminated string.

Ivan Borisov
  • 413
  • 2
  • 7