Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting to safe unicode in python

I'm dealing with unknown data and trying to insert into a MySQL database using Python/Django. I'm getting some errors that I don't quite understand and am looking for some help. Here is the error.

Incorrect string value: '\xEF\xBF\xBDs m...'

My guess is that the string is not being properly converted to unicode? Here is my code for unicode conversion.

s = unicode(content, "utf-8", errors="replace")

Without the above unicode conversion, the error I get is

'utf8' codec can't decode byte 0x92 in position 31: unexpected code byte. You passed in 'Fabulous home on one of Decatur\x92s most

Any help is appreciated!

like image 786
Jesse Shieh Avatar asked Sep 03 '26 06:09

Jesse Shieh


2 Answers

What is the original encoding? I'm assuming "cp1252", from pixelbeat's answer. In that case, you can do

>>> orig # Byte string, encoded in cp1252
'Fabulous home on one of Decatur\x92s most' 

>>> uni = orig.decode('cp1252')
>>> uni # Unicode string
u'Fabulous home on one of Decatur\u2019s most'

>>> s = uni.encode('utf8')  
>>> s # Correct byte string encoded in utf-8
'Fabulous home on one of Decatur\xe2\x80\x99s most'
like image 62
dF. Avatar answered Sep 04 '26 20:09

dF.


0x92 is right single curly quote in windows cp1252 encoding.

\xEF\xBF\xBD is the UTF8 encoding of the unicode replacement character (which was inserted instead of the erroneous cp1252 character).

So it looks like your database is not accepting the valid UTF8 data?

2 options: 1. Perhaps you should be using unicode(content,"cp1252") 2. If you want to insert UTF-8 into the DB, then you'll need to config it appropriately. I'll leave that answer to others more knowledgeable

like image 31
pixelbeat Avatar answered Sep 04 '26 19:09

pixelbeat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!