Python coding - failed to decode before utf8

I have a sqlite database populated with an external program. I am trying to read data using python. When I try to read the data, I get the following error:

OperationalError: Failed to decode UTF-8

If I open the database in sqlite manager and look at the data in the violation record using the built-in view and search, this looks fine, however, if I export the table as csv, I notice the £ symbol in the violation of the steel record  £

If I read csv in python, then the offensive entries still read as  £ , but this is not a problem, I can parse it manually. However, I need to be able to read data directly from the database without the intermediate conversion step to csv.

I looked at some answers on the Internet for similar questions, I still tried to set "text_factory = str", and I also tried to change the column data type from TEXT to BLOB using the SQL manager, but still get an error.

My code below leads to OperationalError: failed to decode UTF-8

conn = sqlite3.connect('test.db')
conn.text_factory = str
curr = conn.cursor()
curr.execute('''SELECT xml_dump FROM hands_1 LIMIT  5000  , 5001''')
row = curr.fetchone()

All entries above 5000 in the database have this problem with the character and therefore generate an error.

Any help was appreciated.

+1
1

Python ( ) python str . , python , ( ), . utf-8. , .

, python , (, Unicode).

conn.text_factory = str

( ), python 3, str - factory, (. ).

, , python , , str, :

your_string = str(the_bytes, 'utf-8') # actually uses `conn.text_factory`, not `str`

... , "utf-8". str, . , :

conn.text_factory = lambda x: str(x, 'latin1')

, python, python , "latin1" "utf-8". , , latin1 . , , . :

  • 'iso-8859-1'
  • 'utf-16'
  • 'utf-32'
  • 'latin1'

.

- , , . , . , :

conn.text_factory = bytes
+11

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


All Articles