python提取数据库数据_Python如何读取MySQL数据库表数据
本文實(shí)例為大家分享了Python讀取MySQL數(shù)據(jù)庫(kù)表數(shù)據(jù)的具體代碼,供大家參考,具體內(nèi)容如下
環(huán)境:Python 3.6 ,Window 64bit
目的:從MySQL數(shù)據(jù)庫(kù)讀取目標(biāo)表數(shù)據(jù),并處理
代碼:
# -*- coding: utf-8 -*-
import pandas as pd
import pymysql
## 加上字符集參數(shù),防止中文亂碼
dbconn=pymysql.connect(
host="**********",
database="kimbo",
user="kimbo_test",
password="******",
port=3306,
charset='utf8'
)
#sql語(yǔ)句
sqlcmd="select col_name,col_type,col_desc from itf_datadic_dtl_d limit 10"
#利用pandas 模塊導(dǎo)入mysql數(shù)據(jù)
a=pd.read_sql(sqlcmd,dbconn)
#取前5行數(shù)據(jù)
b=a.head()
print(b)
# 讀取csv數(shù)據(jù)
# pd.read_csv()
# 讀取excel數(shù)據(jù)
#pd.read_excel()
# 讀取txt數(shù)據(jù)
#pd.read_table()
結(jié)果如圖:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
總結(jié)
以上是生活随笔為你收集整理的python提取数据库数据_Python如何读取MySQL数据库表数据的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: java作业10
- 下一篇: 发生身份验证错误_Python MySQ