安装
pip install sqlalchemy pip install pymysql pip install pandas
导入包
import pandas as pdfrom sqlalchemy import create_engine
连接数据库
engine = create_engine('mysql+pymysql://用户名(一般为root):密码@localhost/数据库名')
直接读取数据
data= pd.read_sql_table('表名',engine)
使用with,我们需要try,except,finally,做异常判断,并且文件最终不管遇到什么情况,都要执行finally f.close()关闭文件,with方法帮我们实现了finally中f.close
with engine.connect() as conn, conn.begin(): data = pd.read_sql_table('表名', conn) data
向数据库中插入数据
df = pd.read_XXX('文件路径').head(10) df.to_sql('表名',engine,if_exists='append')
查看df信息
df.info()
导出csv数据
df.to_csv('/home/shanghaimei/Desktop/name.csv')
作者:_Haimei
链接:https://www.jianshu.com/p/d3b975d729b3
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦