title: cx_Oracle - encoding query result to Raw
body: <p><strong>EDIT:</strong> </p>
  
  <p>The following print shows my intended value.</p>
  
  <p>(both sys.stdout.encoding and sys.stdin.encoding are 'UTF-8').</p>
  
  <p>Why is the variable value different than its print value? I need to get the raw value into a variable.</p>
  
  <pre><code>&gt;&gt;username = 'Jo\xc3\xa3o'
  &gt;&gt;username.decode('utf-8').encode('latin-1')
  'Jo\xe3o'
  &gt;&gt;print username.decode('utf-8').encode('latin-1')
  João
  </code></pre>
  
  <p><strong>Original question:</strong></p>
  
  <p>I'm having an issue querying a BD and decoding the values into Python.</p>
  
  <p>I confirmed my DB NLS_LANG using</p>
  
  <pre><code>select property_value from database_properties where property_name='NLS_CHARACTERSET';
  
  '''AL32UTF8 stores characters beyond U+FFFF as four bytes (exactly as Unicode defines 
  UTF-8). Oracle’s “UTF8” stores these characters as a sequence of two UTF-16 surrogate
  characters encoded using UTF-8 (or six bytes per character)'''
  
  os.environ["NLS_LANG"] = ".AL32UTF8"
  
  ....
  conn_data = str('%s/%s@%s') % (db_usr, db_pwd, db_sid)
  
  sql = "select user_name apex.users where user_id = '%s'" % userid
  
  ...
  
  cursor.execute(sql)
  ldap_username = cursor.fetchone()
  ...
  </code></pre>
  
  <p>where </p>
  
  <pre><code>print ldap_username
  &gt;&gt;'Jo\xc3\xa3o'
  </code></pre>
  
  <p>I've both tried (which return the same)</p>
  
  <pre><code>ldap_username.decode('utf-8')
  &gt;&gt;u'Jo\xe3o'
  unicode(ldap_username, 'utf-8')
  &gt;&gt;u'Jo\xe3o'
  </code></pre>
  
  <p>where</p>
  
  <pre><code>u'João'.encode('utf-8')
  &gt;&gt;'Jo\xc3\xa3o'
  </code></pre>
  
  <p>how to get the queries result back to the proper 'João' ?</p>