Python3: Reading a text/binary mixed file

Paulo da Silva p_s_d_a_s_i_l_v_a_ns at netcabo.pt
Tue Mar 10 00:01:49 EDT 2015


On 10-03-2015 00:55, Dave Angel wrote:
> On 03/09/2015 08:45 PM, Paulo da Silva 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?
>>
...

> 
> In which version of Python?  there's a huge difference between 2.x and 3.x
Python 3. See the subject please.
> 
> Generally speaking, you can treat a piece of a binary (input) file as an
> encoded string, so you want to open the file as binary, locate the part
> that's text, and then explicitly decode the string from that.
> 

That's waht I did. However, I was thinking of some other more efficient
and simple way. For example a command to read text and another to read
bytes.

For .pnm photo files I read the entire file (I needed it in memory
anyway), splited a copy separated by b'\n', got the headers stuff and
then used the original remaining bytes as the photo pixels.
But this is very tricky! I am on linux, but if I ran this program on
windows I needed to change it to "eat" also the '\r'.
In the .pnm case the headers don't have special chars. They fit into
ascii. But in a file who have them it would be also difficult to compute
the consumed length.

Thanks.




More information about the Python-list mailing list