<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN">
<HTML>
<HEAD>
  <META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=UTF-8">
  <META NAME="GENERATOR" CONTENT="GtkHTML/3.13.92">
</HEAD>
<BODY>
I am having a slight problem with UTF-8 output with Python.  I have the following program:<BR>
<BR>
<TT>x = 0</TT><BR>
<BR>
<TT>while x < 0x4000:</TT><BR>
<TT>    print u"This is Unicode code point %d (0x%x): %s" % (x, x, unichr(x))</TT><BR>
<TT>    x += 1</TT><BR>
<BR>
This program works perfectly when run directly:<BR>
<BR>
<TT>mbt@pepper:~/tmp$ python test.py</TT><BR>
<TT>This is Unicode code point 0 (0x0):</TT><BR>
<TT>This is Unicode code point 1 (0x1):</TT><BR>
<TT>This is Unicode code point 2 (0x2):</TT><BR>
<TT>This is Unicode code point 3 (0x3):</TT><BR>
<TT>This is Unicode code point 4 (0x4):</TT><BR>
<TT>This is Unicode code point 5 (0x5):</TT><BR>
<TT>This is Unicode code point 6 (0x6):</TT><BR>
<TT>This is Unicode code point 7 (0x7):</TT><BR>
<TT>This is Unicode code point 8 (0x8):</TT><BR>
<TT>This is Unicode code point 9 (0x9):</TT><BR>
<TT>This is Unicode code point 10 (0xa):</TT><BR>
(... continued)<BR>
<BR>
However, when I attempt to redirect the output to a file:<BR>
<BR>
<TT>mbt@pepper:~/tmp$ python test.py >f</TT><BR>
<TT>Traceback (most recent call last):</TT><BR>
<TT>  File "test.py", line 6, in <module></TT><BR>
<TT>    print u"This is Unicode code point %d (0x%x): %s" % (x, x, unichr(x))</TT><BR>
<TT>UnicodeEncodeError: 'ascii' codec can't encode character u'\x80' in position 39: ordinal not in range(128)</TT><BR>
<BR>
This is slightly confusing to me.  The output goes all the way to the end of the program when it is <I>not</I> redirected.  Why is Python treating the situation differently when the output is redirected?  This failure occurs for all redirection, by the way: >, >>, 1>2, pipes, and so forth.<BR>
<BR>
Any ideas?<BR>
<BR>
    — Mike<BR>
<TABLE CELLSPACING="0" CELLPADDING="0" WIDTH="100%">
<TR>
<TD>
<BR>
--<BR>
<TABLE BGCOLOR="#e9e9e9" CELLSPACING="0" CELLPADDING="0" WIDTH="100%" BORDER="1">
<TR>
<TD>
Michael B. Trausch
</TD>
<TD>
<DIV ALIGN=right><A HREF="mailto:fd0man@gmail.com">fd0man@gmail.com</A></DIV>
</TD>
</TR>
<TR>
<TD WIDTH="50%">
Phone: (404) 592-5746
</TD>
<TD>
<DIV ALIGN=right><TABLE CELLSPACING="0" CELLPADDING="0" WIDTH="289">
<TR>
<TD WIDTH="70" ALIGN="right">
<FONT COLOR="#000000">Jabber IM:</FONT>
</TD>
<TD WIDTH="160" NOWRAP ALIGN="right">
<FONT COLOR="#000000">fd0man@gmail.com</FONT><BR>
<FONT COLOR="#000000">fd0man@livejournal.com</FONT>
</TD>
</TR>
</TABLE></DIV>
</TD>
</TR>
</TABLE>
<B>Demand Freedom!  Use </B><B><I>open</I></B><B> and </B><B><I>free</I></B><B> protocols, standards, and software!</B>
</TD>
</TR>
</TABLE>
</BODY>
</HTML>