Python:使用pandas把excel内容写入数据库

省略安装相关依赖步骤:

import numpy as np
import pandas as pd
from sqlalchemy import create_engine
from sqlalchemy.types import String, Integer
import os
os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'

sheet_name = 'Sheet1'
d = pd.read_excel("data/test.xlsx", sheet_name=sheet_name, dtype='str')
df = d[0:10] # 只写前10行
df = df.drop(columns=['AAA']) # 去掉AAA列

dtype_dic = {col:String(df[col].str.len().max()*2) for col in df.columns }

conn = create_engine('oracle://username:passwd@hostname:1521/dbname')
df.to_sql("tbname", conn, schema='db', if_exists='replace', dtype=dtype_dic, index=False)

你可能感兴趣的:(编程语言/Python)