Sqlite3.OperationalError: Failed to decode UTF-8 column

I have a sqlite database with this line of information, ù should really be "-"

sqlite> select * from t_question where rowid=193;
193|SAT1000|having a pointed, sharp qualityùoften used to describe smells|pungent|lethargic|enigmatic|resolute|grievous

When I read this line from python, I get this error, what am I doing wrong?

Traceback (most recent call last):
  File "foo_error.py", line 8, in <module>
    cur.execute(sql_string)
  sqlite3.OperationalError: Could not decode to UTF-8 column 'posit' with text 'having a pointed, sharp qualityùoften used to describe smells'

Python file:

import sqlite3
conn = sqlite3.connect('sat1000.db')
cur = conn.cursor()
sql_string = 'SELECT * FROM t_question WHERE rowid=193'
cur.execute(sql_string)
conn.close()
+4
source share
2 answers

Set text_factoryto str:

conn = sqlite3.connect('sat1000.db')
conn.text_factory = str

This will cause s curto be returnedstr instead of automatically decoding strwith the codec UTF-8.

, 'ù' , , u'-', u'\xad', u'\u2010', u'\u2011', u'\u2043', u'\ufe63' u'\uff0d', , / . , , str.replace .

:

In [43]: print('ù'.decode('utf-8').encode('cp437').decode('cp1252'))# EM DASH u'\u2014'

, /, 'ù' - .

+17

conn.text_factory = str .

conn.text_factory = bytes. : fooobar.com/questions/1534178/...

+2

Source: https://habr.com/ru/post/1534177/


All Articles