This article mainly introduces the method of reading Excel data and storing it in MySQL with Python3. It has a certain reference value. Now I share it with you. Friends in need can refer to it
Python is A powerful tool for data analysis.
The first step in using Python for data analysis is to learn how to read various excel reports generated in daily work and store them in the data to facilitate subsequent data processing.
Here I would like to share with you how python3 uses xlrd to read excel, and use Python3 to operate the pymysql module to store data in Mysql. Friends in need should take a look.
Preface
pymsql is a module in Python that operates MySQL. Its usage is almost the same as MySQLdb. But currently pymysql supports python3.x and the latter does not support version 3.x.
Python mainly uses the two libraries xlrd and xlwt to operate excel, that is, xlrd is a library for reading excel, and xlwt is a library for writing excel.
Version
python >= 3.6
mysql >= 5.7.19
Installation
The installation of python and mysql will not be described in detail here. Friends who need it can Baidu by themselves
xlrd: You can also use pip to install it. Manually download the source code for installation, pip installation: pip install xlrd
pymysql: You can use pip installation or manually download the source code for installation, pip installation: pip install xlrd
##module
import xlrd import pymysql from datetime import datetime from xlrd import xldate_as_tuple
Read excel
data = xlrd.open_workbook("D:/sales_data.xls") //读取D盘中名为sales_data的excel表格 table_one = data.sheet_by_index(0) //根据sheet索引获取sheet的内容 table_two = data.sheet_by_index(1)
Create database connection
db = pymysql.connect("localhost", "root", "gaishi123", "sales_data", use_unicode=True, charset="utf8")
##gaishi123 is the password of mysql root, sales_data is the database name
for site in sites: # 遍历sheet1 for nrows_one in range(1, int(table_one.nrows)): if table_one.cell_value(nrows_one, 0) == site: payday = table_one.cell_value(0, 8) date = datetime(*xldate_as_tuple(payday, 0)) payday = date.strftime('%Y/%m/%d') # 出票日期 sales = float(table_one.cell_value(nrows_one, 1)) # 销量 quantity_ticket = int(table_one.cell_value(nrows_one, 2)) # 票数 rate_electronic = float(table_one.cell_value(nrows_one, 3)) # 电子直销占比 sales_thanlastweek = float(table_one.cell_value(nrows_one, 4)) # 销量同比上周 sales_thanlastyear = float(table_one.cell_value(nrows_one, 5)) # 销量同比去年 break # 遍历sheet2 for nrows_two in range(1, int(table_two.nrows)): if table_one.cell_value(nrows_two, 0) == site: session = int(table_two.cell_value(nrows_two, 1)) # 访问量 rate_conversion = float(table_two.cell_value(nrows_two, 2)) # 转化率 rate_paysuccess = float(table_two.cell_value(nrows_two, 3)) # 支付成功率 session_thanlastweek = float(table_two.cell_value(nrows_two, 4)) # 访问量同比上周 break # 将数据存入数据库 sql = "insert into sales_data(SITE, PAYDAY, SALES, QUANTITY_TICKET, RATE_ELECTRONIC, SALES_THANLASTWEEK," \ "SALES_THANLASTYEAR, SESSION, SESSION_THANLASTWEEK, RATE_CONVERSION, RATE_PAYSUCCESS)" \ " values ('%s','%s', %f, %d, %f, %f, %f, %d, %f, %f, %f)" %\ (site, payday, sales, quantity_ticket, rate_electronic, sales_thanlastweek, sales_thanlastyear, session, session_thanlastweek, rate_conversion, rate_paysuccess) try: # 使用 cursor() 方法创建一个游标对象 cursor cursor = db.cursor() cursor.execute(sql) except Exception as e: # 发生错误时回滚 db.rollback() print(str(e)) else: db.commit() # 事务提交 print('事务处理成功')
Related recommendations:
The above is the detailed content of How to read Excel data and store it in MySQL using Python3. For more information, please follow other related articles on the PHP Chinese website!

Python and C each have their own advantages, and the choice should be based on project requirements. 1) Python is suitable for rapid development and data processing due to its concise syntax and dynamic typing. 2)C is suitable for high performance and system programming due to its static typing and manual memory management.

Choosing Python or C depends on project requirements: 1) If you need rapid development, data processing and prototype design, choose Python; 2) If you need high performance, low latency and close hardware control, choose C.

By investing 2 hours of Python learning every day, you can effectively improve your programming skills. 1. Learn new knowledge: read documents or watch tutorials. 2. Practice: Write code and complete exercises. 3. Review: Consolidate the content you have learned. 4. Project practice: Apply what you have learned in actual projects. Such a structured learning plan can help you systematically master Python and achieve career goals.

Methods to learn Python efficiently within two hours include: 1. Review the basic knowledge and ensure that you are familiar with Python installation and basic syntax; 2. Understand the core concepts of Python, such as variables, lists, functions, etc.; 3. Master basic and advanced usage by using examples; 4. Learn common errors and debugging techniques; 5. Apply performance optimization and best practices, such as using list comprehensions and following the PEP8 style guide.

Python is suitable for beginners and data science, and C is suitable for system programming and game development. 1. Python is simple and easy to use, suitable for data science and web development. 2.C provides high performance and control, suitable for game development and system programming. The choice should be based on project needs and personal interests.

Python is more suitable for data science and rapid development, while C is more suitable for high performance and system programming. 1. Python syntax is concise and easy to learn, suitable for data processing and scientific computing. 2.C has complex syntax but excellent performance and is often used in game development and system programming.

It is feasible to invest two hours a day to learn Python. 1. Learn new knowledge: Learn new concepts in one hour, such as lists and dictionaries. 2. Practice and exercises: Use one hour to perform programming exercises, such as writing small programs. Through reasonable planning and perseverance, you can master the core concepts of Python in a short time.

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver CS6
Visual web development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.