title: Latin-1 and the unicode factory in Python
body: <p>I have a Python 2.6 script that is gagging on special characters, encoded in Latin-1, that I am retrieving from a SQL Server database. I would like to print these characters, but I'm somewhat limited because I am using a library that calls the <code>unicode</code> factory, and I don't know how to make Python use a codec other than <code>ascii</code>.</p>
  
  <p>The script is a simple tool to return lookup data from a database without having to execute the SQL directly in a SQL editor. I use the <a href="http://code.google.com/p/prettytable/" rel="noreferrer">PrettyTable</a> 0.5 library to display the results.</p>
  
  <p>The core of the script is this bit of code. The tuples I get from the cursor contain integer and string data, and no Unicode data. (I'd use <code>adodbapi</code> instead of <code>pyodbc</code>, which would get me Unicode, but <code>adodbapi</code> gives me other problems.)</p>
  
  <pre><code>x = pyodbc.connect(cxnstring)
  r = x.cursor()
  r.execute(sql)
  
  t = PrettyTable(columns)
  for rec in r:
      t.add_row(rec)
  r.close()
  x.close()
  
  t.set_field_align("ID", 'r')
  t.set_field_align("Name", 'l')
  print t
  </code></pre>
  
  <p>But the <code>Name</code> column can contain characters that fall outside the ASCII range. I'll sometimes get an error message like this, in line 222 of <code>prettytable.pyc</code>, when it gets to the <code>t.add_row</code> call:</p>
  
  <pre><code>UnicodeDecodeError: 'ascii' codec can't decode byte 0xed in position 12: ordinal not in range(128)
  </code></pre>
  
  <p>This is line 222 in <code>prettytable.py</code>. It uses <code>unicode</code>, which is the source of my problems, and not just in this script, but in other Python scripts that I have written.</p>
  
  <pre><code>for i in range(0,len(row)):
      if len(unicode(row[i])) &gt; self.widths[i]:   # This is line 222
          self.widths[i] = len(unicode(row[i]))
  </code></pre>
  
  <p>Please tell me what I'm doing wrong here. How can I make <code>unicode</code> work without hacking <code>prettytable.py</code> or any of the other libraries that I use? Is there even a way to do this?</p>
  
  <p>EDIT: The error occurs not at the <code>print</code> statement, but at the <code>t.add_row</code> call.</p>
  
  <p>EDIT: With Bastien Léonard's help, I came up with the following solution. It's not a panacea, but it works.</p>
  
  <pre><code>x = pyodbc.connect(cxnstring)
  r = x.cursor()
  r.execute(sql)
  
  t = PrettyTable(columns)
  for rec in r:
      urec = [s.decode('latin-1') if isinstance(s, str) else s for s in rec]
      t.add_row(urec)
  r.close()
  x.close()
  
  t.set_field_align("ID", 'r')
  t.set_field_align("Name", 'l')
  print t.get_string().encode('latin-1')
  </code></pre>
  
  <p>I ended up having to decode on the way in and encode on the way out. All of this makes me hopeful that everybody ports their libraries to Python 3.x sooner than later!</p>