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就沒有搞定中文亂碼的問題)

 

謝謝!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM