Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>When running from the console Python can detect the encoding of the console and implicitly converts Unicode printed to the console to that encoding. It can still fail if that encoding doesn't support the characters you are trying to print. UTF-8 can support all Unicode characters, but other common console encodings like cp437 on US Windows don't.</p> <p>When stdout is not a console, Python 2.X defaults to ASCII when it can't determine a console encoding. That's why in a web sever you have to be explicit and encode your output yourself.</p> <p>As an example, try the following script from a console and from your webserver:</p> <pre><code>import sys print sys.stdout.encoding </code></pre> <p>From the console you should get some encoding, but from the web server you should get <code>None</code>. Note that Python 2.X uses <code>ascii</code> but Python 3.X uses <code>utf-8</code> when the encoding cannot be determined.</p> <p>The problem can also occur at a console when redirecting output. This script:</p> <pre><code>import sys print &gt;&gt;sys.stderr,sys.stdout.encoding print &gt;&gt;sys.stderr,sys.stderr.encoding </code></pre> <p>returns the following when run directly vs. redirecting <code>stdout</code>:</p> <pre><code>C:\&gt;test cp437 cp437 C:\&gt;test &gt;out.txt None cp437 </code></pre> <p>Note <code>stderr</code> wasn't affected since it wasn't redirected.</p> <p>The environment variable <code>PYTHONIOENCODING</code> can be used to override the default stdout/stdin encoding as well.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload