简体   繁体   English

使用odo将Pandas Dataframe复制到Postgres

[英]Copy Pandas Dataframe to Postgres with odo

There is this top python library odo that I would love to use to transfer data from Pandas to my postgres db . 我想使用这个顶级python库odo将数据从Pandas传输到我的postgres db。 it looks so convenient but i don't manage to use it :( . 它看起来很方便,但我无法使用它:(。

Has someone an idea what I am missing. 有人知道我想念的是什么。 It must be a little thing ..... 一定是小东西.....

  • Dataframe = df 数据框= df
  • username: postgres 用户名:postgres
  • password: password 密码:密码
  • host : localhost 主机:本地主机
  • table: table_ 表格:table_

So i come with the following ligne of code : 因此,我附带以下代码:

import odo, pandas 
..... 
odo(df, 'postgresql://postgres:password@localhost::mapping_ean_asin') 

I got the error: 我得到了错误:

TypeError: 'module' object is not callable

Thank you in advance for your help :) 预先感谢您的帮助 :)

Solution to my on question :) ! 解决我的问题:)!

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()

I would have prefer the a solution in one line with odo but I got an second error..... 我本来希望用odo与一行解决方案,但是我遇到了第二个错误.....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM