title: Writing Unicode text to a text file?
body: <p>I'm pulling data out of a Google doc, processing it, and writing it to a file (that eventually I will paste into a Wordpress page).</p>
  
  <p>It has some non-ASCII symbols. How can I convert these safely to symbols that can be used in HTML source? </p>
  
  <p>Currently I'm converting everything to Unicode on the way in, joining it all together in a Python string, then doing: </p>
  
  <pre><code>import codecs
  f = codecs.open('out.txt', mode="w", encoding="iso-8859-1")
  f.write(all_html.encode("iso-8859-1", "replace"))
  </code></pre>
  
  <p>There is an encoding error on the last line: </p>
  
  <blockquote>
    <p>UnicodeDecodeError: 'ascii' codec can't decode byte 0xa0 in position
    12286: ordinal not in range(128)</p>
  </blockquote>
  
  <p><strong>Partial solution:</strong></p>
  
  <p>This Python runs without an error:</p>
  
  <pre><code>row = [unicode(x.strip()) if x is not None else u'' for x in row]
  all_html = row[0] + "&lt;br/&gt;" + row[1]
  f = open('out.txt', 'w')
  f.write(all_html.encode("utf-8"))
  </code></pre>
  
  <p>But then if I open the actual text file, I see lots of symbols like:</p>
  
  <pre><code>Qur‚Äôan 
  </code></pre>
  
  <p>Maybe I need to write to something other than a text file? </p>