Python——读取MySQL数据并导出到Excel
原创
©著作权归作者所有:来自51CTO博客作者前方一片光明的原创作品,请联系作者获取转载授权,否则将追究法律责任
Step1 安装pandas、openpyxl、sqlalchemy
pip install pandas openpyxl sqlalchemy
Step2 代码
import pandas as pd
from sqlalchemy import create_engine
# engine = create_engine('dialect+driver://username:password@host:port/database')
engine = create_engine('mysql+pymysql://root:*********@localhost:3306/stock')
sql = "SELECT * FROM dic_bk"
data = pd.read_sql(sql, engine)
print(data.head())
data.to_excel('d:/dic_bk.xlsx', index=None)
print("success")