Extended ASCII [solved]

D'Arcy Cain darcy at vex.net
Fri Jan 13 19:30:58 EST 2017


On 2017-01-13 05:44 PM, Grant Edwards wrote:
> On 2017-01-13, D'Arcy Cain <darcy at VybeNetworks.com> wrote:
>
>> Here is the failing code:
>>
>> with open(sys.argv[1], encoding="latin-1") as fp:
>>    for ln in fp:
>>      print(ln)
>>
>> Traceback (most recent call last):
>>    File "./load_iff", line 11, in <module>
>>      print(ln)
>> UnicodeEncodeError: 'ascii' codec can't encode character '\xe9' in
>> position 132: ordinal not in range(128)
>>
>> I don't understand why the error says "ascii" when I told it to use
>> "latin-1".
>
> That can't be the failing code, since it's failing at line 11, and
> that's only 5 lines. It helps if we can tell which line generated the
> error.  ;)

I didn't think that the part leading up to it was relevant.  Here it is.

#! /usr/bin/python

import sys, os

if len(sys.argv) < 2:
     print("No file named", file=sys.stderr)
     sys.exit(1)

> I'm _guessing_ that line 11 is the print(), and it's barfing because

Of course.  That's why the error is listed right below it.

> stdout is using ascii encoding, and there's no way to encode that
> character in ascii so that it can be printed to an ascii output
> stream.

Thank you!  I know all about that but for some reason I did not have 
PYTHONIOENCODING=utf8 set on that particular machine.  I have it set on 
every other machine I work on an never thought to check.  My Unicode 
universe is all right again.

Cheers.

-- 
D'Arcy J.M. Cain
System Administrator, Vex.Net
http://www.Vex.Net/ IM:darcy at Vex.Net
VoIP: sip:darcy at Vex.Net


More information about the Python-list mailing list