title: How do I write data to disk in UTF-8 encoding in Python?
body: <p>The following Python code ...</p>
  
  <pre><code>html_data = urllib2.urlopen(some_url).read()
  f = codecs.open(filename, 'w', encoding='utf-8')
  f.write(html_data)
  f.close()
  </code></pre>
  
  <p>... sometimes fails with <code>UnicodeDecodeError</code> ...</p>
  
  <pre><code>File "/.../lib/python2.6/codecs.py", line 686, in write
    return self.writer.write(data)
  File "/.../lib/python2.6/codecs.py", line 351, in write
    data, consumed = self.encode(object, self.errors)
  UnicodeDecodeError: 'ascii' codec can't decode byte 0xd0 in position 5605: ordinal not in range(128)
  </code></pre>
  
  <p>My questions:</p>
  
  <ul>
  <li>How do I make sure my <code>urllib2.urlopen(some_url).read()</code> call always returns UTF-8?</li>
  <li>Is there anything wrong with my <code>codecs.open(...)</code> call that prevents it from storing my data to disk in UTF-8 encoding?</li>
  </ul>