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())

輸出結果:mysql

          數據庫                        數據表          表註釋  數據大小(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就沒有搞定中文亂碼的問題)sql

 

謝謝!數據庫

相關文章
相關標籤/搜索