[Twisted-Python] print unicode

Phil Mayers p.mayers at imperial.ac.uk
Wed May 5 06:47:38 MDT 2010


On 05/05/10 13:31, Pet wrote:
> On Wed, May 5, 2010 at 2:05 PM, Maarten ter Huurne
> <maarten at treewalker.org>  wrote:
>> On Wednesday 05 May 2010, Pet wrote:
>>
>>> I'm using Twisted 10.0 and as usually sometime print debug infos with
>>> myunicodestr.encode('UTF-8') which are saved to logfile, but since
>>> using twisted 10 I'm getting
>>>
>>> UnicodeEncodeError: 'ascii' codec can't encode characters...
>>
>> UTF-8 uses the full 8 bits of a byte, while ASCII only uses 7, so writing
>> Unicode encoded as UTF-8 to an ASCII stream is not valid.
>>
>> I think recent Python versions are more strict about what is written to
>> stdout/stderr than older versions, it might not be related to Twisted
>> itself. You can specify a different encoding for stdin/out/err by setting
>> the PYTHONIOENCODING environment variable.
>
> Hi Maarten!
>
> Thanks for help!
> Unfortunately, my Python installation is 2.5.2 and PYTHONIOENCODING is
> introduced in 2.6

I think this is highly dependent on your OS environment. For example:

Python 2.4.3 (#1, Oct 23 2006, 14:19:47)
[GCC 4.1.1 20060525 (Red Hat 4.1.1-1)] o
Type "help", "copyright", "credits" or "
 >>> import sys
 >>> sys.getdefaultencoding()
'ascii'
 >>> print unichr(163)
£


[pjm3 at wildfire ~]$ echo $LANG
en_GB.UTF-8

...but:

LANG=C python
Python 2.4.3 (#1, Oct 23 2006, 14:19:47)
[GCC 4.1.1 20060525 (Red Hat 4.1.1-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
 >>> print unichr(163)
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
UnicodeEncodeError: 'ascii' codec can't encode character u'\xa3' in 
position 0: ordinal not in range(128)

...i.e. here I can just print unicode characters, with nothing 
particularly special, provided my environment variables are set right.




More information about the Twisted-Python mailing list