title: Python 3: How to specify stdin encoding
body: <p>While porting code from Python 2 to Python 3, I run into this problem when reading UTF-8 text from standard input. In Python 2, this works fine:</p>
  
  <pre><code>for line in sys.stdin:
      ...
  </code></pre>
  
  <p>But Python 3 expects ASCII from <em>sys.stdin</em>, and if there are non-ASCII characters in the input, I get the error:</p>
  
  <blockquote>
    <p>UnicodeDecodeError: 'ascii' codec can't decode byte .. in position ..: ordinal not in range(128)</p>
  </blockquote>
  
  <p>For a regular file, I would specify the encoding when opening the file:</p>
  
  <pre><code>with open('filename', 'r', encoding='utf-8') as file:
      for line in file:
          ...
  </code></pre>
  
  <p>But how can I specify the encoding for standard input? Other SO posts (e.g. <a href="https://stackoverflow.com/questions/2737966/how-to-change-the-stdin-encoding-on-python">How to change the stdin encoding on python</a>) have suggested using</p>
  
  <pre><code>input_stream = codecs.getreader('utf-8')(sys.stdin)
  for line in input_stream:
      ...
  </code></pre>
  
  <p>However, this doesn't work in Python 3. I still get the same error message. I'm using Ubuntu 12.04.2 and my locale is set to en_US.UTF-8.</p>