我正在尝试将dataframe
批量上传到数据库表中。我有以下代码,该代码首先将dataframe
写入csv
,然后将csv
的内容复制到数据库表中。我遇到的错误是psycopg2.OperationalError: could not open file "C:\Users\test\PycharmProjects\etc\demand_override.csv" for reading: No such file or directory
,但是当我将以上目录复制并粘贴到资源管理器中时,csv文件会打开!我在哪里做错了?
from sqlalchemy import create_engine
import pandas as pd
import time
import os
engine = create_engine("postgresql://myapp:mypass@project.com/my_database")
schema = 'scenario_1'
df = pd.read_csv('./input/demand_override.csv')
t2 = time.time()
cwd = os.getcwd()
df.to_csv(cwd+'/'+'demand_override.csv', index=False)
copy_query = "COPY " + schema + "." + 'demand_override' + " FROM '" + cwd + '\\' + 'demand_override.csv' + "' CSV HEADER"
_connection = engine.connect()
_connection.execute(copy_query)