Home > Article > Backend Development > Introduction to the method of reading MySQL table data in Python
This article mainly introduces in detail how Python reads MySQL database table data. It has certain reference value. Interested friends can refer to it.
The examples in this article share with you Python reading The specific code to get the MySQL database table data is for your reference. The specific content is as follows
Environment: Python 3.6, Window 64bit
Purpose: Read the target table data from the MySQL database and process
Code:
# -*- coding: utf-8 -*- import pandas as pd import pymysql ## 加上字符集参数,防止中文乱码 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数据 a=pd.read_sql(sqlcmd,dbconn) #取前5行数据 b=a.head() print(b) # 读取csv数据 # pd.read_csv() # 读取excel数据 #pd.read_excel() # 读取txt数据 #pd.read_table()
The result is as shown:
[Related recommendations]
1. Special recommendation:"php Programmer Toolbox" V0.1 version download
3. Video tutorial on the application of Python in data science
The above is the detailed content of Introduction to the method of reading MySQL table data in Python. For more information, please follow other related articles on the PHP Chinese website!