我很想用这个顶级 python 库 odo 将数据从 Pandas 传输到我的 postgres 数据库。它看起来很方便,但我无法使用它:(。
有人知道我缺少什么吗?一定是小东西.....
- 数据框 = df
- 用户名:postgres
- 密码:密码
- 主机:本地主机
- 表:table_
所以我带来了以下代码行:
import odo, pandas
.....
odo(df, 'postgresql://postgres:password@localhost::mapping_ean_asin')
我得到了错误:
TypeError: 'module' object is not callable
预先感谢您的帮助:)
最佳答案
解决我的问题 :) !
SQL_STATEMENT = """ COPY %s FROM STDIN WITH
CSV
HEADER
DELIMITER AS ','
"""
my_file = open("/home.........csv")
def process_file(conn, table_name, file_object):
cursor = conn.cursor()
cursor.copy_expert(sql=SQL_STATEMENT % table_name, file=file_object)
conn.commit()
cursor.close()
connection = psycopg2.connect("dbname='postgres' user='postgres' host='localhost' password='password'")
try:
process_file(connection, 'my table', my_file)
finally:
connection.close()
我更喜欢 odo 在一行中的解决方案,但我遇到了第二个错误......
关于python - 使用 odo 将 Pandas Dataframe 复制到 Postgres,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52874495/