本文實例為大家分享了Python讀取MySQL數(shù)據(jù)庫表數(shù)據(jù)的具體代碼,供大家參考,具體內容如下
環(huán)境:Python 3.6 ,Window 64bit
目的:從MySQL數(shù)據(jù)庫讀取目標表數(shù)據(jù),并處理
代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
# -*- 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語句 sqlcmd = "select col_name,col_type,col_desc from itf_datadic_dtl_d limit 10" #利用pandas 模塊導入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() |
結果如圖:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。