title: Curious about unicode / string encoding in Python 3
body: <p>I'd like to ask why something works which I have found after painful hours of reading/trying to understand and in the end simply succesfull trial-and-error...</p>
  
  <p>I'm on Linux (Ubuntu 13.04, German time formats etc., but english system language). My small python 3 script connects to a sqlite3 database of the reference manager Zotero. There I read a couple of keys with the goal of exporting files from the zotero storage directory (probably not important, and as said above, got it working).</p>
  
  <p>All of this works fine with characters in the ascii set, but of course there are a lot of international authors in the database and my code used to fail on non-ascii authors/paper titles.
  Perhaps first some info about the database on command line sqlite3:</p>
  
  <pre><code>sqlite3 zotero-test.sqlite 
  SQLite version 3.7.15.2 2013-01-09 11:53:05
  sqlite&gt; PRAGMA encoding;
  UTF-8
  </code></pre>
  
  <p>Exemplary problematic entry:</p>
  
  <pre><code>sqlite&gt; select * from itemattachments;   
  317|281|1|application/pdf|5|storage:MÃ¼ller-Forell_2008_Orbitatumoren.pdf||2|1372357574000|2814ef3ea9c50cce2c32d6fb46b977bb
  </code></pre>
  
  <p>The correct name would be "storage:Müller-Forrell"; Zotero itself decodes this correctly, but SQLIte does not (at least dos not output it correctly in my terminal).</p>
  
  <p>Google tells me that "Ã¼" is a somehow incorrectly or not decoded latin-1/8859-1 "ü".</p>
  
  <p>Reading this database entry in from python3 with</p>
  
  <pre><code>connection = sqlite3.connect("zotero-test.sqlite")`
  cursor = connection.cursor()`
  cursor.execute("SELECT itemattachments.itemID,itemattachments.sourceItemID,itemattachments.path,items.key FROM itemattachments,items WHERE mimetype=\"application/pdf\" AND items.itemID=itemattachments.itemID")
  
  for pdf_result in cursor:
      print(pdf_result[2])
      print()
      print(pdf_result[2].encode("latin-1").decode("utf-8"))
  </code></pre>
  
  <p>gives:</p>
  
  <pre><code>storage:MÃ¼ller-Forell_2008_Orbitatumoren.pdf
  
  storage:Müller-Forell_2008_Orbitatumoren.pdf
  </code></pre>
  
  <p>, the second being correct, so I got my script working (gosh how many hours this cost me...)</p>
  
  <p>Can somebody explain to me what this construction of .encode and .decode does? Which one is even executed first?
  Thanks for any clues,</p>
  
  <p>Joost</p>