import mysql.connector
import pandas as pd
# 连接数据库
cnx = mysql.connector.connect(user='', password='',
host='', database='', port='')
cursor = cnx.cursor()
# 读取 Excel 文件并转换为 DataFrame 对象
with pd.ExcelFile('Excel文件名') as xls:
df = pd.read_excel(xls, sheet_name='Sheet1')
# 遍历 DataFrame 对象并插入数据到 MySQL 数据库
for index, row in df.iterrows():
ip = row['IP']
user = row['User']
sql = "INSERT INTO 表名 (ip, user) VALUES (%s, %s)"
val = (ip, user)
cursor.execute(sql, val)
# 提交更改并关闭连接
cnx.commit()
cursor.close()
cnx.close()
Python脚本----Excel文件处理后导入数据库
最新推荐文章于 2024-08-31 21:39:38 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)