Python, Unicode, and the Windows console
<p>When I try to print a Unicode string in a Windows console, I get a <code>UnicodeEncodeError: 'charmap' codec can't encode character ....</code> error.  I assume this is because the Windows console does not accept Unicode-only characters. What's the best way around this? Is there any way I can make Python automatically print a <code>?</code> instead of failing in this situation?</p>

<p><strong>Edit:</strong>  I'm using Python 2.5.</p>

<hr>

<p><strong>Note:</strong> @LasseV.Karlsen answer with the checkmark is sort of outdated (from 2008). Please use the solutions/answers/suggestions below with care!!</p>

<p><a href="http://stackoverflow.com/a/32176732/610569">@JFSebastian answer</a> is more relevant as of today (6 Jan 2016).</p>

<p><strong>Note:</strong> This answer is sort of outdated (from 2008). Please use the solution below with care!!</p>

<hr>

<p>Here is a page that details the problem and a solution (search the page for the text <em>Wrapping sys.stdout into an instance</em>):</p>

<p><a href="http://wiki.python.org/moin/PrintFails">PrintFails - Python Wiki</a></p>

<p>Here's a code excerpt from that page:</p>

<pre><code>$ python -c 'import sys, codecs, locale; print sys.stdout.encoding; \
    sys.stdout = codecs.getwriter(locale.getpreferredencoding())(sys.stdout); \
    line = u"\u0411\n"; print type(line), len(line); \
    sys.stdout.write(line); print line'
  UTF-8
  &lt;type 'unicode'&gt; 2
  Ð
  Ð

  $ python -c 'import sys, codecs, locale; print sys.stdout.encoding; \
    sys.stdout = codecs.getwriter(locale.getpreferredencoding())(sys.stdout); \
    line = u"\u0411\n"; print type(line), len(line); \
    sys.stdout.write(line); print line' | cat
  None
  &lt;type 'unicode'&gt; 2
  Ð
  Ð
</code></pre>

<p>There's some more information on that page, well worth a read.</p>
