title: How to import mysql data to .txt file using python 3.5?
body: <p>I am trying to import mysql data into a .txt file using python 3.x but it look like I'm missing something.The expectation is, data should be imported to a file in tabular/columns format. I tried my level best to get solution but I'm not getting what I need.</p>
  
  <p>Below is my code :</p>
  
  <pre><code>import pymysql.cursors
  import pymysql
  import sys
  import os
  
  # Connect to the database
  connection = pymysql.connect(host='localhost',
                               user='root',
                               password="",
                               db='jmeterdb',
                               cursorclass=pymysql.cursors.DictCursor)
  
  try:
      with connection.cursor() as cursor:
          # Select all records
          sql = "select * from emp"
          cursor.execute(sql)
  
      # connection is not autocommit by default. So you must commit to save
      # your changes.
      result = cursor.fetchall()
      newfile = open("db-data.txt","a+")
      for row in result:
          newfile.writelines(row)
  
      print(result)
      newfile.close()
  
  finally:
      connection.close()
  </code></pre>
  
  <p>On terminal python shows me data when <code>print(result)</code> is executed but in the <code>db-data.txt</code> file, it shows column-names only.</p>
  
  <p>Expected result :</p>
  
  <pre><code>Column_Name1 Column_Name2 Column_Name3
  data1        data2        data3
  data1        data2        data3
  </code></pre>