使用csv写入数据库时​​没有此类文件或目录

时间:2018-11-17 19:33:06

标签: python pandas

我正在尝试将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)

0 个答案:

没有答案