title: SQLAlchemy and UnicodeDecodeError
body: <p>I'm getting</p>
  
  <pre><code>UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 0: ordinal not in range(128)
  </code></pre>
  
  <p>when I pass text coming from a MySQL database, which I am accessing using SQLAlchemy, to this function:</p>
  
  <pre><code>re.compile(ur"&lt;([^&gt;]+)&gt;", flags=re.UNICODE).sub(u" ", s)
  </code></pre>
  
  <p>The database encoding is utf-8 and I am even passing the encoding to the create_engine function of SQLAlchemy.</p>
  
  <p><strong>Edit:</strong>
  This is how I am querying the database:</p>
  
  <pre><code>doc = session.query(Document).get(doc_id)
  s = doc.title
  </code></pre>
  
  <p>By suggestion, I passed s.decode('utf-8') to <code>sub</code> . The error above disappeared, but I get a different error for a different document:</p>
  
  <pre><code>UnicodeDecodeError: 'utf8' codec can't decode byte 0xeb in position 449: invalid continuation byte
  </code></pre>
  
  <p>The database table is defined like this:</p>
  
  <pre><code>CREATE TABLE `articles` (
    `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
    `title` varchar(255) DEFAULT NULL,
    `cdate` datetime DEFAULT NULL,
    `link` varchar(255) DEFAULT NULL,
    `content` text,
    UNIQUE KEY `id` (`id`),
    UNIQUE KEY `link_idx` (`link`)
  ) ENGINE=InnoDB AUTO_INCREMENT=4127834 DEFAULT CHARSET=utf8;
  </code></pre>
  
  <p>Any help would be greatly appreciated</p>