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 18:38:38 EDT 2015


On 10-03-2015 04:14, Cameron Simpson wrote:
> On 10Mar2015 04:01, Paulo da Silva <p_s_d_a_s_i_l_v_a_ns at netcabo.pt> wrote:
>> On 10-03-2015 00:55, Dave Angel wrote:
...
>> 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'.
> 
> If you're in Python 3 (recommended!) and you're parsing the headers as
> text, you should be converting your split binary into strings anyway. So
> you can just use .strip() or rstrip(); either will remove trailing '\r'
> and '\n', so it will work in both UNIX and Windows.
> 
I didn't know strip removes \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.
> 
> I presume you're gathering the headers in "binary" mode and decoding
> each to a string. So you know the consumed length from the binary half;
> that they're different lengths after decoding to strings is then
> irrelevant.
You are right.
I am still a little confused about python3.

Thanks a lot




More information about the Python-list mailing list