I tried running the pgmrand.py program and receive the following errors:
Traceback (most recent call last):
File "H:\PDF_OpenStuff\OReillyBooks\Python\RWIWP_Code\CH03\pgmrand.py", line 50, in
File "C:\Python32\lib\encodings\cp1252.py", line 19, in encode
UnicodeEncodeError: 'charmap' codec can't encode character '\x80' in position 19: character maps to
Was wondering what is missting.
Help get this topic noticed by sharing it on Twitter, Facebook, or email.
I have forwarded your question on to our Book Tech staff, so they can help you find a solution. If you ever have any questions or issues with the content of a title, you can also contact them directly at firstname.lastname@example.org or call them at:
1-707-827-7019 (outside the USA)
7:30 am to 5:00 PST
The author responded with the note below, and I have sent the file he refers to directly to your email address.
O'Reilly Book Support
Interesting. The original code I have runs fine and generates a random PGM image file as it should.
The error tells me that somehow a value > 255 is being written into the image array ('\x80' = 256). This is no longer a byte (i.e a character), but would be an integer word (at least 16 bits). So, somehow, the range limit check on line 38 seems to have failed.
I would suggest that the reader verify that he's using at least version 2.6 of Python (or 2.7, I don't know if 3.x will have issues--I've not tried it, to be honest, but I can't see why it wouldn't work). Also, if he copied and pasted from a PDF, it may have also copied some extraneous characters. These are perfectly valid ASCII characters, but they're outside the "normal" set used for source code, so Python doesn't know what to do with them. The problem could actually be somewhere further up in the code, not just on line 50 where the interpreter caught it.
I've had issues with this sort of thing in the past myself.
I've attached an ASCII file with the original example program, which you are welcome to pass along to the reader. I just ran it and it works fine.
If it still has issues, and the Python version is 2.6 or 2.7, then I would suggest he look at what he is using for an editor. A word processor is bound to cause problems and Windows notepad is a disaster, but something like Eclipse, UltraEdit (which I use), Kate, or Geany will work just fine. Eclipse, Kate, and Geany are OSS, UltraEdit is commercial, but not too expensive. Of course, vi or emacs will also work just fine. Basically, any editor that only uses the base set of printable ASCII characters and plain old straight quotes and apostrophe glyphs should work fine.