Home > Backend Development > Python Tutorial > Introduction to the method of reading MySQL table data in Python

Introduction to the method of reading MySQL table data in Python

Y2J
Release: 2017-05-15 11:05:16
Original
1714 people have browsed it

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()
Copy after login

The result is as shown:

[Related recommendations]

1. Special recommendation:"php Programmer Toolbox" V0.1 version download

2. Python free video tutorial

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template