Python operation MySQL Chinese display garbled solution _python

Source: Internet
Author: User

This example shows a script python used to convert the table configuration data XML and generate the corresponding parsing code.
But in the Chinese code on the emergence of garbled, the solution is to share out for everyone's reference.

The specific methods are as follows:

1. python File Set encoding Utf-8 (#encoding =utf-8 before file)
2. mysql Database charset=utf-8
3. Python connection MySQL is a plus parameter Charset=utf8
4. Set the default encoding for Python to Utf-8 (sys.setdefaultencoding (Utf-8)

The sample code is as follows:

Copy Code code as follows:
#encoding =utf-8

Import Sys
Import MySQLdb as MDB

Reload (SYS)
Sys.setdefaultencoding (' Utf-8 ')

con = None

Try
Con = mdb. Connect (' localhost ', ' root ', ' jobin ', ' zmld ', charset= ' UTF8 ')
cur = con.cursor ()
Cur.execute ("Show all columns from player")

numrows = Int (cur.rowcount)

For I in Range (NumRows):
row = Cur.fetchone ()
Comment = Row[len (row)-1]
Print Comment
Finally
If con:
Con.close ()

I hope this article will help you with your Python programming.

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.