Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

How to open and present raw binary data in Python?

This seems to be the type of question that should have a lot of duplicates and plenty of answers, but my searches have led only to frustration and no useable solutions.

In Python (preferably 3.x), I would like to know how I can open a file of an arbitrary type, read the bytes that are stored on disk, and present those bytes in their most 'native', 'original', 'raw' form, before any encoding is done on them.

If the file is stored on disk as a stream of 00010100 10000100 ... then that's what I would like to have presented on the screen.

These sort of questions usually elicit the response 'why do you want to know' and 'what's the use case'. I'm curious, that's my use case.

Before you mark this as duplicate, please be sure that the answer you have in mind does indeed answer the question (rather than merely discuss encodings, etc.). Thank you!

EDIT AFTER FIRST THREE ANSWERS:

Thanks to the three responders up to this point, and especially to J.F. Sebastian for the extended discussion. It appears from what has been said that my question boils down to how bytes in files are physically recorded to disk and how they can be read and presented. At this point it doesn't seem possible in Python to obtain a view on to the bytes in their raw form, but they are available in various representations; integers, hex values, ascii, etc. As the matter isn't settled, I will leave the question open for more input.

Answer*

Draft saved
Draft discarded
Cancel
1
  • 1
    Just convert it to a list: list(b'abc')[97, 98, 99]. You can also access every element by index b'abc'[1]98. Commented Oct 15, 2015 at 14:19

lang-py

AltStyle によって変換されたページ (->オリジナル) /