MySQL in Python: UnicodeEncodeError: 'ascii'

Hello, I'm trying to save a string value for MySQL, and I'm using db.escape_string () to not reset special characters. Line

Lala*=#&%@<>_?!:;-'"/()¥¡¿ 

But when I try to run the code, I get this error:

UnicodeEncodeError: codec 'ascii' cannot encode characters at positions 23-25: serial number not in range (128)

What should I do?

+4
source share
2 answers

Try http://pypi.python.org/pypi/Unidecode/0.04.1

For instance:

 from unidecode import unidecode your_string = 'Lala*=#&%@<>_?!:;-\'"/()¥¡¿' unidecode(your_string) 

Note that I escaped a character from your string to avoid a SyntaxError

0
source

The error says that there are characters that do not exist in ASCII (they are unicode) Try using:

 newStr = u'Lala*=#&%@<>_?!:;-'"/()¥¡¿' 

It should be declared as Unicode, and u'something instead.
This hardly works with most python shells, so make sure your IDE supports Unicode and that the file you are using has a unicode declaration at the top.

-2
source

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


All Articles