title: How can I create a file with utf-8 in Python?
body: <p>I use <code>open('test.txt', 'w')</code> to create a new file and its character set is <strong>binary</strong>.
  </p>
  
  <pre><code>&gt;&gt;&gt; open('test.txt', 'w')
  &lt;open file 'test.txt', mode 'w' at 0x7f6b973704b0&gt;
  
  $ file -i test.txt.txt 
  test2.txt: inode/x-empty; charset=binary
  </code></pre>
  
  <p>Assign a file with the specified character set (say <code>utf-8</code>) using the module <code>codecs</code>. However, the charset is still <strong>binary</strong>.</p>
  
  <p></p>
  
  <pre><code>&gt;&gt;&gt; codecs.open("test.txt", 'w', encoding='utf-8')
  &lt;open file 'test.txt', mode 'wb' at 0x7f6b97370540&gt;
  
  $ file -i test.txt 
  test.txt: inode/x-empty; charset=binary
  </code></pre>
  
  <p>I write something to <code>test.txt</code> and the charset is <strong>us-ascii</strong>.
  </p>
  
  <pre><code>&gt;&gt;&gt; fp. write ("wwwwwwwwwww")
  &gt;&gt;&gt; fp.close()
  
  $ file -i test.txt 
  test.txt: text/plain; charset=us-ascii
  </code></pre>
  
  <p>OK, now, I write some <em>special</em> characters (say <code>Arènes</code>). However, 
  </p>
  
  <pre><code>&gt;&gt;&gt; fp = codecs.open("test.txt", 'w', encoding='utf-8')
  &gt;&gt;&gt; fp.write("Arènes")
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
    File "/usr/lib/python2.7/codecs.py", line 688, in write
      return self.writer.write(data)
    File "/usr/lib/python2.7/codecs.py", line 351, in write
      data, consumed = self.encode(object, self.errors)
  UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 2: ordinal not in range(128)
  </code></pre>
  
  <hr>
  
  <p>To be more specific, I would like to save the query result (using <code>python-mysqldb</code>) into a file. The key source codes are as follows:
  </p>
  
  <pre><code>cur.execute("SELECT * FROM agency")
  
  # Write to a file
  with open('test.txt', 'w') as fp :
      for row in cur.fetchall() :
          s = '\t'.join(str(item) for item in row)
          fp.write(s + '\n')
  </code></pre>
  
  <p>Now, the charset of <code>test.txt</code> is <strong>iso-8859-1</strong> (some French characters, such as <code>Arènes</code>). </p>
  
  <p>Thus, I use <code>codecs.open('test.txt', 'w', encoding='utf-8')</code> to create a file. However, encounter the following error:</p>
  
  <p></p>
  
  <pre><code>Traceback (most recent call last):
    File "./overlap_intervals.py", line 26, in &lt;module&gt;
      fp.write(s + '\n')
    File "/usr/lib/python2.7/codecs.py", line 688, in write
      return self.writer.write(data)
    File "/usr/lib/python2.7/codecs.py", line 351, in write
      data, consumed = self.encode(object, self.errors)
  UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 21: ordinal not in range(128)
  </code></pre>
  
  <p><strong>How can I create a file with utf-8 in Python?</strong></p>