title: Usage of unicode() and encode() functions in Python
body: <p>I have a problem with encoding of the <em>path</em> variable and inserting it to the <em>SQLite</em> database. I tried to solve it with <em>encode(&quot;utf-8&quot;)</em> function which didn't help. Then I used <code>unicode()</code> function which gives me type <em>unicode</em>.</p>
  <pre><code>print type(path)                  # &lt;type 'unicode'&gt;
  path = path.replace(&quot;one&quot;, &quot;two&quot;) # &lt;type 'str'&gt;
  path = path.encode(&quot;utf-8&quot;)       # &lt;type 'str'&gt; strange
  path = unicode(path)              # &lt;type 'unicode'&gt;
  </code></pre>
  <p>Finally I gained <em>unicode</em> type, but I still have the same error which was present when the type of the <em>path</em> variable was <em>str</em></p>
  <blockquote>
  <p>sqlite3.ProgrammingError: You must not use 8-bit bytestrings unless
  you use a text_factory that can interpret 8-bit bytestrings (like
  text_factory = str). It is highly recommended that you instead just
  switch your application to Unicode strings.</p>
  </blockquote>
  <p>Could you help me solve this error and explain the correct usage of <code>encode(&quot;utf-8&quot;)</code> and <code>unicode()</code> functions? I'm often fighting with it.</p>
  <p>This <code>execute()</code> statement raised the error:</p>
  <pre><code>cur.execute(&quot;update docs set path = :fullFilePath where path = :path&quot;, locals())
  </code></pre>
  <p>I forgot to change the encoding of <code>fullFilePath</code> variable which suffers with the same problem, but I'm quite confused now. Should I use only <code>unicode()</code> or <code>encode(&quot;utf-8&quot;)</code> or both?</p>
  <p>I can't use</p>
  <pre><code>fullFilePath = unicode(fullFilePath.encode(&quot;utf-8&quot;))
  </code></pre>
  <p>because it raises this error:</p>
  <blockquote>
  <p>UnicodeDecodeError: 'ascii' codec can't decode byte 0xc5 in position
  32: ordinal not in range(128)</p>
  </blockquote>
  <p><em>Python</em> version is <em>2.7.2</em></p>