0

getting the error while reading data from DB2 table using PYTHON 3.x.

UnicodeDecodeError: 'ascii' codec can't decode byte 0xc5 in position 10: ordinal not in range(128)

sayan_sen
  • 345
  • 4
  • 15
  • Can you try to read file with `encoding='utf-8'` parameter? – talatccan Jan 07 '20 at 11:28
  • 1
    Does this answer your question? [Python and IBM DB2: UnicodeDecodeError](https://stackoverflow.com/questions/37395235/python-and-ibm-db2-unicodedecodeerror) – mao Jan 07 '20 at 11:28
  • no . I did not find anything for python 3.x. Can you please help with an answer – sayan_sen Jan 07 '20 at 12:09
  • EDIT your question to add *all* environment and version details. What's your Db2-server platform and version ? What's your database-code page and territory ? What operating-system runs Python 3.x? Which Db2-client are you using and what is its version? What's the version of the ibm_db module ? What's the column-datatype being fetched? Show your *code*, or make a reproducible example. – mao Jan 07 '20 at 12:28
  • 1
    It may be recommended to review: https://stackoverflow.com/questions/18649512/unicodedecodeerror-ascii-codec-cant-decode-byte-0xe2-in-position-13-ordinal – hidehy Jan 09 '20 at 07:38

1 Answers1

0

By default, both Python and your ODBC driver are looking at your locale settings in Linux to determine your default encoding.

First, set your locale settings as follows:

export LC_ALL=C.UTF-8

Second, instead of using string literals in your SQL queries, use parameters:

for example, change

c.execute("INSERT INTO table (data) VALUES ('non-ascii stuff')"

to

c.execute("INSERT INTO table (data) VALUES (?)", ['non-ascii stuff'])
soundstripe
  • 1,454
  • 11
  • 19