Y
Y
York Princess2018-06-13 08:55:56
Python
York Princess, 2018-06-13 08:55:56

How to achieve Cyrillic support in Python3 flask + mysql?

Hello. When designing the simplest web api in Python3, I ran into a problem:

  • i use flash
  • i use mariadb
  • i do cursor.execute then cursor.fetchall
  • i get a result tuple in which all cyrillic characters are replaced by question marks
  • accordingly, flask.jsonify(result) also produces json with question marks
  • with Latin and numbers everything is in order

5b20b11677bc9545164396.png
Actually the question is what to do with it, and is it possible to do something at all? Or will it be easier to quickly rewrite from a beautiful Python to something more terrible, but working?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
Y
York Princess, 2018-06-14
@Princess_York

The solution was kindly provided in the comments by user pcdesign .
To read the required encoding from the database, the following version of the pymysql.connect() method was used:

db = pymysql.connect(host='localhost',
                             user='myuser',
                             passwd='welcome',
                             db='mydb',
                             charset='utf8')

This solved half the problem - now everything was read from the database as it should, but flask displayed Unicode characters simply by displaying their code. To solve this adversity, the following approach was taken:
app = Flask(__name__)
app.config['JSON_AS_ASCII'] = False

From that moment on, flask.jsonify started rendering Cyrillic characters as they are.

S
Sergey Gornostaev, 2018-06-13
@sergey-gornostaev

You should definitely start by reading the Unicode HOWTO of the Python documentation. And be sure to make sure that the base encoding is UTF-8.

I
Ilya, 2018-06-14
@nuBacuk

The documentation says that you must explicitly specify the encoding. charset=utf8, example below and documentation.
# set client encoding to utf8; all strings come back as unicode
docs.sqlalchemy.org/en/latest/dialects/mysql.html#...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question