Python3: Reading a text/binary mixed file

Chris Angelico rosuav at gmail.com
Mon Mar 9 20:56:21 EDT 2015


On Tue, Mar 10, 2015 at 11:45 AM, Paulo da Silva
<p_s_d_a_s_i_l_v_a_ns at netcabo.pt> wrote:
> Hi!
>
> What is the best way to read a file that begins with some few text lines
> and whose rest is a binary stream?
>
> As an exmaple ... files .pnm.
>
> Thanks for any comments/help on this.

Read the entire file in binary mode, and figure out which parts are
text and how they're encoded (possibly ASCII or UTF-8). Then take just
those snippets, and decode them. Something like this:

data = open("some_file", "rb")
text_part = data[2718:3142]
decoded_text = text_part.decode("utf-8")

That'll give you a usable Unicode string, assuming you have your
offsets and encoding correct.

ChrisA



More information about the Python-list mailing list