title: Python : UnicodeEncodeError: 'latin-1' codec can't encode character
body: <p>I am at a scenario where I call api and based on the results from api I call database for each record that I in api. My api call return strings and when I make the database call for the items return by api, for some elements I get the following error. </p>
  
  <pre><code>Traceback (most recent call last):
    File "TopLevelCategories.py", line 267, in &lt;module&gt;
      cursor.execute(categoryQuery, {'title': startCategory});
    File "/opt/ts/python/2.7/lib/python2.7/site-packages/MySQLdb/cursors.py", line 158, in execute
      query = query % db.literal(args)
    File "/opt/ts/python/2.7/lib/python2.7/site-packages/MySQLdb/connections.py", line 265, in literal
      return self.escape(o, self.encoders)
    File "/opt/ts/python/2.7/lib/python2.7/site-packages/MySQLdb/connections.py", line 203, in unicode_literal
      return db.literal(u.encode(unicode_literal.charset))
  UnicodeEncodeError: 'latin-1' codec can't encode character u'\u2013' in position 3: ordinal not in range(256)
  </code></pre>
  
  <p>The segment of my code the above error is referring is:</p>
  
  <pre><code>         ...    
           for startCategory in value[0]:
              categoryResults = []
              try:
                  categoryRow = ""
                  baseCategoryTree[startCategory] = []
                  #print categoryQuery % {'title': startCategory}; 
                  cursor.execute(categoryQuery, {'title': startCategory}) #unicode issue
                  done = False
                  cont...
  </code></pre>
  
  <p>After doing some google search I tried the following on my command line to understand whats going on...</p>
  
  <pre><code>&gt;&gt;&gt; import sys
  &gt;&gt;&gt; u'\u2013'.encode('iso-8859-1')
  Traceback (most recent call last):
    File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  UnicodeEncodeError: 'latin-1' codec can't encode character u'\u2013' in position 0: ordinal not in range(256)
  &gt;&gt;&gt; u'\u2013'.encode('cp1252')
  '\x96'
  &gt;&gt;&gt; '\u2013'.encode('cp1252')
  '\\u2013'
  &gt;&gt;&gt; u'\u2013'.encode('cp1252')
  '\x96'
  </code></pre>
  
  <p>But I am not sure what would be the solution to overcome this issue. Also I don't know what is the theory behind <code>encode('cp1252')</code> it would be great if I can get some explanation on what I tried above. </p>