On 03/09/2015 08:56 PM, Chris Angelico wrote:
On Tue, Mar 10, 2015 at 11:45 AM, Paulo da Silva
<p_s_d_a_s_i_l_v_a...@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")

You accidentally omitted the part where you read() the data

data = data.read()

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



--
DaveA
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to