The Python Flask framework and Mysql database are used, and the database ORM adopts the SQLAlchemy framework. For solutions to Chinese garbled characters, see the following code snippet:
@ App. route ('/', methods = ['get'])
The code is as follows: |
Copy code |
Def index (): Games_origin = DimGameName. query. all () Games = [] For g in games_origin: Game = {} Game ['value'] = g. value. encode ('latin1'). decode ('gbk ') Game ['id'] = g. id Game ['appid '] = g. appid Games. append (game) Return flask.render_template('index.html ', games = games) |
Mainly used: g. value. encode ('latin1'). decode ('gbk ')
Different encoding and decoding methods are used for different database encoding configurations. The following is my Mysql database encoding configuration (latin1 ):
When you use navicat to connect to mysql and use GBK (GB2312) in the connection settings, the Chinese data displayed in the query is normal,
Therefore, you can use encode ('latin1'). decode ('gbk') to solve Chinese problems.