Package: python2.4
Version: 2.4.4-2
Severity: normal
Hello,
Thanks for maintaining Python.
While I can output unicode properly on a terminal, the same script does
not work when redirected to a file:
$ cat test.py
#! /usr/bin/python
# vim:set encoding=utf-8:
import sys
def output(s):
print s.decode('utf-8')
print sys.stdout.encoding
s = "ééé"
output("%s" % (s))
$ ./test.py
UTF-8
ééé
$ ./test.py > test
Traceback (most recent call last):
File "./test.py", line 12, in ?
output("%s" % (s))
File "./test.py", line 7, in output
print s.decode('utf-8')
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2:
ordinal not in range(128)
$ cat test
None
A work-around for this is (courtesy of MadCoder):
import sys, codecs
if sys.stdout.encoding is None:
sys.stdout = codecs.open("/dev/stdout", "w", 'utf-8')
Ciao,
Enrico
-- System Information:
Debian Release: 4.0
APT prefers testing
APT policy: (500, 'testing')
Architecture: amd64 (x86_64)
Shell: /bin/sh linked to /bin/bash
Kernel: Linux 2.6.20.1enrico
Locale: LANG=it_IT.UTF-8, LC_CTYPE=it_IT.UTF-8 (charmap=UTF-8)
Versions of packages python2.4 depends on:
ii libbz2-1.0 1.0.3-6 high-quality block-sorting file co
ii libc6 2.3.6.ds1-13 GNU C Library: Shared libraries
ii libdb4.4 4.4.20-8 Berkeley v4.4 Database Libraries [
ii libncursesw5 5.5-5 Shared libraries for terminal hand
ii libreadline5 5.2-2 GNU readline and history libraries
ii libssl0.9.8 0.9.8c-4 SSL shared libraries
ii mime-support 3.39-1 MIME files 'mime.types' & 'mailcap
ii python2.4-minimal 2.4.4-2 A minimal subset of the Python lan
python2.4 recommends no packages.
-- no debconf information