Python page garbled Problem and Solution (pycharm + mysql)

Source: Internet
Author: User
Tags mysql code
Under Ubuntu: thereisnodoubtthat is caused by encoding problems. Ensure that the encoding of mysql, pycharm and browsers is three-in-one. 1. Add a line of "#-*-coding: UTF-8-*-" class at the beginning of each python page :#! Usrbinpython #-*-coding: UTF-8-*-2, modify the pycharm encoding: setting-

Under Ubuntu: there is no doubt that is caused by encoding problems. Ensure that the encoding of mysql, pycharm and browsers is three-in-one. 1. Add a line of "#-*-coding: UTF-8-*-" class at the beginning of each python page :#! /Usr/bin/python #-*-coding: UTF-8-*-2, modify the pycharm encoding: setting-

Under Ubuntu:

There is no doubt that is caused by encoding problems. Ensure that the encoding of mysql, pycharm, and browser is three-in-one.


1. Add a line "#-*-coding: UTF-8-*-" at the beginning of each python page -*-"

Similar to this :#! /Usr/bin/python

#-*-Coding: UTF-8 -*-

2. Modify the pycharm encoding:

Setting-> File Encoding-> All Encoding methods are changed to UTF-8 (some tutorials on the Internet say IDE Encoding should be changed to GBK, because in windows, the default Encoding is GBK, while in ubuntu is utf8)


3. Modify the mysql encoding:

View your mysql code: After logging on, enter the command show variables like '% char %', for example, mysql> show variables like '% char %' and the default mysql code is displayed:

You can manually use the command set @ character_set_client = utf8 to modify it, but it does not seem permanent. We recommend that you directly modify the configuration file/etc/mysql/my. cnf.

Find the [client] and [mysql] and add the default-character-set = utf8,

Find [mysqld] and add this section below it

Init_connect = 'set collation_connection = utf8_unicode_ci'
Init_connect = 'set NAMES utf8'
Character-set-server = utf8
Collation-server = utf8_unicode_ci
Skip-character-set-client-handshake

Save and exit.

Restart mysql. Enter sudo service mysql restart in the shell command line

After the restart, log on to mysql and check the encoding (command show variables like '% char %'). UTF-8 is returned, except for filesystem (My character_set_database or GBK, but it does not seem to have any impact)

The tutorial for modifying the mysql configuration file found on the Internet is to add the default-character-set = utf8 and init_connect = 'set NAMES utf8' sentences under [mysqld, however, it turns out that mysql cannot be restarted after it is added. You may try it.


4. Modify the browser encoding. Generally, Firefox is used in ubuntu. The modification method is as follows:

Edit-> preferences-> content-> font and color "advanced" button-> default character encoding changed to UTF8


Add: Add charset = 'utr8' to the python database connection'
For example:

Conn = MySQLdb. connect (host = 'localhost', user = 'root', passwd = '000000', db = 'studentdb', charset = 'utf8 ')

This can solve the problem of programming garbled characters After Python inserts Mysql.


If all of the above are correct and garbled characters still occur, try to check:

1. On the Firefox browser page, right-click to view the page information-> General-> to see if the encoding is utf8. If not, click View-> character encoding-> utf8 in the upper-left corner of Firefox browser.


2. If the character code on the page is utf8, make sure that the data stored in mysql is not garbled: log on to mysql and check whether the data in your table is garbled. mysql-> select * from yourTableName, in case of garbled characters, use the insert into statement to insert Chinese characters, and then check whether the data is properly displayed in the browser.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.