簡體   English   中英

使用odo將Pandas Dataframe復制到Postgres

[英]Copy Pandas Dataframe to Postgres with odo

我想使用這個頂級python庫odo將數據從Pandas傳輸到我的postgres db。 它看起來很方便,但我無法使用它:(。

有人知道我想念的是什么。 一定是小東西.....

  • 數據框= 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與一行解決方案,但是我遇到了第二個錯誤.....

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM