Python之pandas读取mysql中文乱码问题

# -*- coding: utf-8 -*-
# author:baoshan

import pandas as pd
import pymysql
config = {
    "host": "localhost",
    "port": 3306,
    "user": "root",
    "password": "123456",
    "charset": "utf8"
}
conn = pymysql.connect(**config)
sql = "select " \
      "table_schema as '数据库'," \
      "table_name as '数据表', " \
      "TABLE_COMMENT as '表注释', " \
      "round(data_length/1024/1024,2) as '数据大小(M)', " \
      "round(index_length/1024/1024,2) as '索引大小(M)', " \
      "TABLE_ROWS as '行数'" \
      "from information_schema.tables " \
      "where TABLE_SCHEMA not in ('information_schema', 'performance_schema', 'mysql') " \
      "AND TABLE_ROWS > 0"

df = pd.read_sql(sql, conn)
print(df.head())

输出结果:

          数据库                        数据表          表注释  数据大小(M)  索引大小(M)    行数
0  collection          colt_data_element          信息元     0.02     0.02    16
1  collection  colt_data_element_catalog        数据元目录     0.02     0.00    12
2  collection           colt_depart_user      部门包含的员工     0.02     0.03    16
3  collection            colt_department          委办局     0.02     0.00     6
4  collection          colt_report_audit  信息上报状态流水状态表     0.09     0.02  1072

pandas配合pymysql好用(pandas配合sqlalchemy就没有搞定中文乱码的问题)

谢谢!

猜你喜欢

转载自www.cnblogs.com/zhzhang/p/11386968.html