2

I'm using python 2.7 with jaydebeapi and informix jdbc driver (ifxjdbc.jar) to execute a select on a table and all the fields have u'some_text' Here's my code

curs = conn.cursor()
curs.execute("SELECT * FROM table1")
res = curs.fetchall()
print res

using

res_final=[str(x) for x in res[0]]
print res_final

converts the first line in the result correctly. Any idea how can I convert all the selected rows? I had the same problem with sqlite3 but adding

conn.text_factory=str 

solved my problem. I can't find something that works in the same way for jaydebeapi.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
albgz
  • 23
  • 1
  • 3

1 Answers1

3

If you absolutely need to encode Unicode values to bytestrings, try to do so as late as possible, e.g. when pushing the values somewhere that can only handle bytes, like a file or network socket.

You can encode all columns in all rows with a nested list comprehension:

res = [col.encode('utf8') if isinstance(col, unicode) else col for col in row]
       for row in curs.fetchall()]

which only encodes values that are actually Unicode strings, in a manner that'll work with all Unicode codepoints.

If however all you are concerned about is the u'' prefix, then there is absolutely no need to encode. Don't confuse a type indicator in container representations for actual data.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343