python读取excel csv数据插入Oracle

from sqlalchemy import create_engine
import pandas as pd
import sqlalchemy.types as type
from sqlalchemy.engine import Engine


def connet_oracle():
    ip_post = 'oracle+cx_oracle://{user}:{passwd}@{ip_post_ocl}?charset=utf8'.format(user=user, passwd=passwd,
                                                                                 ip_post_ocl=url)
    return create_engine(ip_post, echo=False, encoding='utf-8')  # ,encoding = "UTF-8", nencoding = "UTF-8"

engine:Engine = connet_oracle()
user='xxx'
passwd='xxx'
url = 'x.x.x.x:1521/xxx'
df = pd.read_excel('./data/3.xls',dtype=str)
# data.to_csv("./data/df.csv")
print(df.head())
# 空值处理
# df[df=='nan'] = None
# df.fillna('',inplace=True)
print(df.shape)
data:pd.DataFrame = df.copy()

# 指定字段类型插入
col_type:dict = {'TABLE_NAME':type.VARCHAR(256)}
data.to_sql('table_name(字母必须小写)', con=engine,if_exists='replace',index=False,chunksize=1000,dtype=col_type)

  

posted @ 2023-03-24 09:20  洺剑残虹  阅读(351)  评论(0编辑  收藏  举报