如何通过csv文件将特定列复制到PostgreSQL

时间:2018-08-10 10:47:33

标签: python postgresql

import psycopg2
import time


def read_database():
    conn = None
    try:
        conn = psycopg2.connect(database="capitadb", user="capita_user", password="capita_user",
                                host = "127.0.0.1", port = "5432")
        cur = conn.cursor()
        start_time = time.time()
        cur.execute("COPY stagging(Activity_ID,F_Qtr,Fiscal_Week_Num,Manager,MBadge) FROM '/home/vivek/Downloads/dell_data.csv'  DELIMITER',' CSV;;")

        print("--- %s seconds ---" % (time.time() - start_time))
        print("Operation done successfully")
        conn.commit()

    except Exception as e:
        print("Error: %s" % e)
    finally:
        conn.close()

if __name__ == '__main__':
    read_database()

在这里,csv文件中有15列,但是我们只想复制4列。我们如何在不提取任何文件数据的情况下实现这一目标?

1 个答案:

答案 0 :(得分:0)

您将需要使用COPY FROM STDIN功能-http://initd.org/psycopg/docs/cursor.html#cursor.copy_from。您将能够为该功能提供类似文件的对象。您可以为此使用itertools模块

from itertools import chain, islice
class some_magic_adaptor(object):
    def __init__(self, src):
        self.src = chain.from_iterable(src)
    def read(self, n):
        return "".join(islice(self.src, None, n))


def read_csv():
    for line in open(csv_filename):
        yield transform_line(line)

file_like_object_for_postgresql = some_magic_adaptor(read_csv())