From: John W. <jb...@be...> - 2004-10-28 02:58:24
|
I found a workaround for the UnicodeEncodeError messages I was getting when I tried to print strings returned from my web service's operations. I should mention that I'm running Mac OS X (10.3) with the default Python 2.3 installation. When I tried print a string stored in the variable (i.e., print response), I would get an error like this: Traceback (most recent call last): File "./DescriptionOfTheIkeaMonkIndex.py", line 15, in ? print response UnicodeEncodeError: 'ascii' codec can't encode character '\u2019' in position 57: ordinal not in range(128) To work around the problem, I did this: if type(response) == unicode: response = response.encode('utf-8') print response John On Oct 27, 2004, at 10:33 AM, Joshua Boverhof wrote: > This works fine on my machine, it seems like the utf-8 codec is not > found on lookup. > So I would try to specify the encoding by putting the following magic > comment at line 2. > > # -*- coding: utf-8 -*- > > > -josh > > > John Ward wrote: > >> Hi Josh, >> >> Thanks for looking into this. I commented out lines 85, 86, and 87 >> and it seems to have solved the TypeError problem. >> >> The only possible snag for me is that I get a new error >> (UnicodeEncodeError) when I try to access the string that's returned >> from the operation. The problem only occurs when I try to print the >> string stored in the variable "response" on line 14 of my test >> script. >> >> Here's the test script I'm using to invoke the operation: >> >> 1 #!/usr/bin/python >> 2 >> 3 from IKEAMonkIndexService_services import * >> 4 >> 5 import sys >> 6 >> 7 loc = IKEAMonkIndexServiceLocator() >> 8 # prints messages sent and recieved if tracefile is set >> 9 kw = { 'tracefile' : sys.stdout } >> 10 >> 11 port = loc.getIKEAMonkIndexServiceSOAPType(**kw) >> 12 request = getDescriptionOfTheIKEAMonkIndexWrapper() >> 13 response = >> port.getDescriptionOfTheIKEAMonkIndexOperation(request) >> 14 print response > > |