title: Working with unicode encoded Strings from Active Directory via python-ldap
body: <p>I already came up with this problem, but after some testing I decided to create a new question with some more specific Infos:</p>
  
  <p>I am reading user accounts with python-ldap (and Python 2.7) from our Active Directory. This does work well, but I have problems with special chars. They do look like UTF-8 encoded strings when printed on the console. The goal is to write them into a MySQL DB, but I don't get those strings into proper UTF-8 from the beginning.</p>
  
  <p>Example (fullentries is my array with all the AD entries):</p>
  
  <pre><code>fullentries[23][1].decode('utf-8', 'ignore')    
  print fullentries[23][1].encode('utf-8', 'ignore')
  print fullentries[23][1].encode('latin1', 'ignore')
  print repr(fullentries[23][1])
  </code></pre>
  
  <p>A second test with a string inserted by hand as follows:</p>
  
  <pre><code>testentry = "M\xc3\xbcller"
  testentry.decode('utf-8', 'ignore')
  print testentry.encode('utf-8', 'ignore')
  print testentry.encode('latin1', 'ignore')
  print repr(testentry)
  </code></pre>
  
  <p>The output of the first example ist:</p>
  
  <pre><code>M\xc3\xbcller
  M\xc3\xbcller
  u'M\\xc3\\xbcller'
  </code></pre>
  
  <p>Edit: If I try to replace the double backslashes with .replace('\\\\','\\) the output remains the same.</p>
  
  <p>The output of the second example:</p>
  
  <pre><code>Müller
  M�ller
  'M\xc3\xbcller'
  </code></pre>
  
  <p>Is there any way to get the AD output properly encoded? I already read a lot of documentation, but it all states that LDAPv3 gives you strictly UTF-8 encoded strings. Active Directory uses LDAPv3.</p>
  
  <p>My older question this topic is here: <a href="https://stackoverflow.com/questions/6202726/writing-utf-8-string-to-mysql-with-python">Writing UTF-8 String to MySQL with Python</a></p>
  
  <p>Edit: Added repr(s) infos</p>