Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Python always encodes Unicode strings when writing to a terminal, file, pipe, etc. When writing to a terminal Python can usually determine the encoding of the terminal and use it correctly. When writing to a file or pipe Python defaults to the 'ascii' encoding unless explicitly told otherwise. Python can be told what to do when piping output through the <code>PYTHONIOENCODING</code> environment variable. A shell can set this variable before redirecting Python output to a file or pipe so the correct encoding is known.</p> <p>In your case you've printed 4 uncommon characters that your terminal didn't support in its font. Here's some examples to help explain the behavior, with characters that are actually supported by my terminal (which uses cp437, not UTF-8).</p> <h2>Example 1</h2> <p>Note that the <code>#coding</code> comment indicates the encoding in which the <em>source file</em> is saved. I chose utf8 so I could support characters in source that my terminal could not. Encoding redirected to stderr so it can be seen when redirected to a file.</p> <pre><code>#coding: utf8 import sys uni = u'αßΓπΣσµτΦΘΩδ∞φ' print &gt;&gt;sys.stderr,sys.stdout.encoding print uni </code></pre> <h3>Output (run directly from terminal)</h3> <pre><code>cp437 αßΓπΣσµτΦΘΩδ∞φ </code></pre> <p>Python correctly determined the encoding of the terminal.</p> <h3>Output (redirected to file)</h3> <pre><code>None Traceback (most recent call last): File "C:\ex.py", line 5, in &lt;module&gt; print uni UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-13: ordinal not in range(128) </code></pre> <p>Python could not determine encoding (None) so used 'ascii' default. ASCII only supports converting the first 128 characters of Unicode.</p> <h3>Output (redirected to file, PYTHONIOENCODING=cp437)</h3> <pre><code>cp437 </code></pre> <p>and my output file was correct:</p> <pre><code>C:\&gt;type out.txt αßΓπΣσµτΦΘΩδ∞φ </code></pre> <h2>Example 2</h2> <p>Now I'll throw in a character in the source that isn't supported by my terminal:</p> <pre><code>#coding: utf8 import sys uni = u'αßΓπΣσµτΦΘΩδ∞φ马' # added Chinese character at end. print &gt;&gt;sys.stderr,sys.stdout.encoding print uni </code></pre> <h3>Output (run directly from terminal)</h3> <pre><code>cp437 Traceback (most recent call last): File "C:\ex.py", line 5, in &lt;module&gt; print uni File "C:\Python26\lib\encodings\cp437.py", line 12, in encode return codecs.charmap_encode(input,errors,encoding_map) UnicodeEncodeError: 'charmap' codec can't encode character u'\u9a6c' in position 14: character maps to &lt;undefined&gt; </code></pre> <p>My terminal didn't understand that last Chinese character.</p> <h3>Output (run directly, PYTHONIOENCODING=437:replace)</h3> <pre><code>cp437 αßΓπΣσµτΦΘΩδ∞φ? </code></pre> <p>Error handlers can be specified with the encoding. In this case unknown characters were replaced with <code>?</code>. <code>ignore</code> and <code>xmlcharrefreplace</code> are some other options. When using UTF8 (which supports encoding all Unicode characters) replacements will never be made, but the <em>font</em> used to display the characters must still support them.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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