python-3.x - 来自 psycopg2 的 ExecuteMany 函数出错
问题描述
我正在尝试从 excel 中提取数据,然后在 python 中使用 psycopg2 将数据插入到 postgres 数据库中。我正在使用 executemany 插入数据库并将 insert sql 语句及其值作为参数传递给它。它抛出错误'字符串索引超出范围'
import pandas as pd
import numpy as np
import psycopg2 as psycopg2
import csv
'''
sample values is:
('123924902','ABC_DEF_GHI_NG',current_user,now() at time zone 'utc',current_user,now() at time zone 'utc')
'''
def extract_from_file(excel_file, sheet_index, row_for_header, output_file):
df = pd.read_excel(excel_file,sheet_name=sheet_index, header=None)
new_header = df.iloc[row_for_header]
##print(new_header)
df = df[row_for_header+1:]
df.columns=new_header
rows_to_insert = []
for index,row in df.iterrows():
##print(row['Sector'])
if pd.isnull(row['ENTITY_ID']):
continue##or break if we
##are confident no intervening empty rows
if not pd.isnull(row['ENTITY_ID']):
row_to_insert = ["('"+str(row['ENTITY_ID'])+"'",
"'"+str(row['SECT_CD'])+"'),"]
row_to_insert = ','.join(row_to_insert)
rows_to_insert.append(row_to_insert)
rows_to_insert[-1] = rows_to_insert[-1][:-1]
with open(output_file,'w') as out:
for row in rows_to_insert:
out.write(row+'\n')
extract_from_file('EntityTableImportTest.xlsx', 0,0, 'EntityTableImportTest.txt')
def insert_entity_list():
""" insert multiple entities into the entity table """
sql = "INSERT into esg.entity(entity_id,sector_key,creat_by,creat_dtm,last_upd_by,last_upd_dtm) VALUES(%s,(select sector_key from esg.sector where sector_cd=%s),current_user,now() at time zone 'utc',current_user,now() at time zone 'utc')"
conn = None
with open('EntityTableImportTest.txt','r') as out:
entity_list = out.read()
print(entity_list)
try:
conn = psycopg2.connect(host="localhost",database="abc",user="abcde",password="abcdefg")
# create a new cursor
print(conn)
cur = conn.cursor()
cur.executemany(sql,entity_list)
# commit the changes to the database
conn.commit()
# close communication with the database
cur.close()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
insert_entity_list()
有人可以帮忙吗?
解决方案
推荐阅读
- java - 查找应用程序方法的平均执行时间?
- python - 如何打印进入训练器(火炬)的输入数据?
- javascript - 在动态页面中加载正确路径时出现问题
- java - 如何从 Spring Cloud Stream Kafka Binder 中的偏移量获取消息?
- plsql - 如何将图像多部分/表单数据从 pl/sql 发布到 Web 服务?
- javascript - 在单页设计的导航栏中突出显示当前部分
- cmake - 捕获 FetchContent_Declare 失败
- angular - 如何在 ts 文件中将数据从一个组件传递到另一个组件?
- ios - 带有卷积的垂直边缘检测,使用 Swift 给出透明图像
- annotations - Modelica 注释导数:noDerivative 与 zeroDerivative