How to write pbm file in Python 3?

Andre Roberge andre.roberge at gmail.com
Sun Oct 31 05:40:02 CET 2010


On Sun, Oct 31, 2010 at 1:32 AM, Chris Rebert <clp2 at rebertia.com> wrote:

> On Sat, Oct 30, 2010 at 9:21 PM, André <andre.roberge at gmail.com> wrote:
> > On Oct 31, 1:11 am, Chris Rebert <c... at rebertia.com> wrote:
> >> On Sat, Oct 30, 2010 at 8:32 PM, André <andre.robe... at gmail.com> wrote:
> >> > I'm trying to create pbm (portable bitmap) files using Python 3 and
> >> > have run into a problem.   The example I am using is the Python 2
> >> > Mandelbrot program found at
> >> >http://shootout.alioth.debian.org/u32/program.php?test=mandelbrot&lan.
> ..
> >>
> >> > When I run it using Python 2 with size=100, I get a file of size 1311
> >> > bytes.  This file contains the correct image.
> >>
> >> > When I run it using Python 3 with the same parameter (and replacing
> >> > xrange by range - the only change suggested by 2to3), I get a file of
> >> > size 1812 bytes; this file does not contain the right image.
> >>
> >> > Any help would be much appreciated.
> >>
> >> Have you tried changing all instances of chr(<whatever>) to
> >> bytes([<whatever>]) ?
> > Yes, I had.  And I got the following traceback:
> >
> > Traceback (most recent call last):
> >  File "mandel_orig.py", line 39, in <module>
> >    main()
> >  File "mandel_orig.py", line 30, in main
> >    cout(bytes([byte_acc]))
> > TypeError: must be str, not bytes
>
> Ah, right. This is the problem of not having Python 3 to test with.
> Try also changing:
> cout = sys.stdout.buffer.write
>

Yes, that worked.  Thanks!

Now I know what to look for, time to read the documentation for
sys.stdout.buffer ...   Thanks again.

André


>
> Cheers,
> Chris
> --
> http://blog.rebertia.com
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20101031/ddc01685/attachment.html>


More information about the Python-list mailing list