python读取Excel到mysql

常见问题:

1.数据库密码有特殊字符

使用urllib.parse.quote_plus 编译密码

mysql_engine = create_engine((f"mysql+pymysql://root:%s@10.0.0.2:3306/mydb")%urllib.parse.quote_plus("pass@=aaaa"))

2.设置字段类型

设置特定类型,和指定日期类型

data1 = pd.read_excel("C:/work/20230818***/数据导入.xlsx",dtype={0:"int", 2:"str", 3:"str"},parse_dates=4,5)

eg:

from sqlalchemy import create_engine

import sqlalchemy

import pandas as pd

import urllib.parse

import datetime

from io import StringIO

use this func, you need to change the cancel , which below # change the test or proc env

be sure you have encoded the tag, and truncate the old db data

if name == 'main':

data1 = pd.read_excel("C:/work/20230818***/数据导入.xlsx",dtype={0:"int", 2:"str", 3:"str"},parse_dates=4,5)

set Create_Time

get current time

now_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")

data1"Create_Time" = now_time

set special column

data1"Oper_Sign" = 'admin'

change the test or proc env

mysql_engine = create_engine(f"mysql+pymysql://账号名:密码@IP地址:3306/数据库名")

mysql_engine = create_engine((f"mysql+pymysql://root:%s@10.0.0.2:3306/mydb")%urllib.parse.quote_plus("pass@=aaaa"))

data1.to_sql(name='表名', con=mysql_engine, if_exists="append", index=False)

print('done')

相关推荐
auto_go18 分钟前
Python 实战指南(7)——账本动不动就崩?先把“魔法字符串”和“裸报错”干掉
python
Beyond_System|系统之外19 分钟前
【学编程】Python基础编程题100道(21-60)
开发语言·python·算法
根目录下的猫34 分钟前
RK3588适配的轻量级AI模型推荐
人工智能·后端·python·目标检测
wno7041 小时前
Spring Security权限控制
java·python·spring
ShineWinsu2 小时前
对于Coze—AI:SDK的解析
人工智能·python·ai·sdk·项目·coze·字节跳动
右耳朵猫AI2 小时前
Python周刊2026W38 | 标准流编码修复、PEP 845/846 草案、解析器提速 10%、集合字典二次复杂度
python·ai·数据科学
微软技术分享2 小时前
大模型网络结构:模型接口测试用例参考
python
Bruce_Liuxiaowei3 小时前
Python 实例赋值遮蔽类属性:一个从不报错的静默陷阱
开发语言·python·语法糖
做运维的阿瑞3 小时前
Python 标准库汇总:分类速览与常用模块清单
linux·运维·python
why-geo4 小时前
Hermes Agent 与 Python 的会产生什么样的碰撞
人工智能·python·ai编程