On 2018-05-19 13:28, bartc wrote:
On 19/05/2018 12:38, Chris Angelico wrote:
On Sat, May 19, 2018 at 8:33 PM, bartc <b...@freeuk.com> wrote:

But then you are acknowledging the file is, in fact, ASCII.

Cool! So what happens if you acknowledge that a file is ASCII, and
then it starts with a byte value of E3 ?

It depends.

If this is a .ppm file I'm trying to read, and it starts with anything
other than 'P' followed by one of '1','2','3','4','5','6' (by which I
mean the ASCII codes for those), then it's a bad ppm file.

What are you really trying to say here?

Out of interest, how would Python handle the headers for binary file
formats P4, P5, P6? I'd have a go but I don't want to waste half the day
trying to get past the language.

It is quite possible to deal with files, including files which are
completely or partially text, a byte at a time, without having silly
restrictions put on them by the language.

Here's the palaver I had to go through last time I wrote such a file
using Python, and this is just for the header:

      s="P6\n%d %d\n255\n" % (hdr.width, hdr.height)
      sbytes=array.array('B',list(map(ord,s)))
      f.write(sbytes)

Was there a simple way to write way to do this? Most likely, but you
have to find it first! Here's how I write it elsewhere:

    println @f, "P6"
    println @f, width,height
    println @f, 255

It's simpler because it doesn't get tied up in knots in trying to make
text different from bytes, bytearrays or array.arrays.

It's very simple:

    s = b"P6\n%d %d\n255\n" % (hdr.width, hdr.height)
    f.write(s)
--
https://mail.python.org/mailman/listinfo/python-list

Reply via email to