title: UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' in position 47: ordinal not in range(128)
body: <p>I am using Python 2.7 and MySQLdb 1.2.3. I tried everything I found on stackoverflow and other forums to handle encoding errors my script is throwing.
  My script reads data from all tables in a source MySQL DB, writes them in a python <code>StringIO.StringIO</code> object, and then loads that data from <code>StringIO</code> object to Postgres database (which apparently is in UTF-8 encoding format. I found this by looking into Properties--Definition of database in pgadmin) using psycopg2 library's copy_from command.</p>
  
  <p>I found out that my source MySQL database has some tables in latin1_swedish_ci encoding while others in utf_8 encoding format (Found this from TABLE_COLLATION in information_schema.tables).</p>
  
  <p>I wrote all this code on the top of my Python script based on my research on the internet.</p>
  
  <pre><code>db_conn = MySQLdb.connect(host=host,user=user,passwd=passwd,db=db, charset="utf8", init_command='SET NAMES UTF8' ,use_unicode=True) 
  db_conn.set_character_set('utf8') 
  db_conn_cursor = db_conn.cursor()
  db_conn_cursor.execute('SET NAMES utf8;')
  db_conn_cursor.execute('SET CHARACTER SET utf8;')
  db_conn_cursor.execute('SET character_set_connection=utf8;')
  </code></pre>
  
  <p>I still get the <code>UnicodeEncodeError</code> below with this line: <code>cell = str(cell).replace("\r", " ").replace("\n", " ").replace("\t", '').replace("\"", "") #Remove unwanted characters from column value</code>, </p>
  
  <pre><code>UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' in position 47: ordinal not in range(128)
  </code></pre>
  
  <p>I wrote the following line of code to clean cells in every table of source MySQL database when writing to StringIO object.</p>
  
  <pre><code>cell = str(cell).replace("\r", " ").replace("\n", " ").replace("\t", '').replace("\"", "") #Remove unwanted characters from column value
  </code></pre>
  
  <p>Please help.</p>