Re: psycopg2 open file for reading

From: "Shulgin, Oleksandr" <oleksandr(dot)shulgin(at)zalando(dot)de>
To: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>
Cc: Dan Sawyer <dansawyer(at)earthlink(dot)net>, psycopg(at)postgresql(dot)org
Subject: Re: psycopg2 open file for reading
Date: 2015-10-28 14:42:26
Message-ID: CACACo5SNtSO5vwoFw26fZe2PwWv0WBXfGFJYcbFHGnry364y0Q@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: psycopg

On Wed, Oct 28, 2015 at 3:35 PM, Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>
wrote:

>
> The cheat is to read the on disk file and write it into an in memory file
> and then use that with STDIN. Something like:
>
> sql_copy = "COPY " + self.pg_tbl_name
> sql_copy += " FROM STDIN WITH CSV DELIMITER '\t'"
> cur_copy.copy_expert(sql_copy, mem_file)
>

And you don't actually need a "memory file", any object implementing "read"
method, such as a normal fie object should just work:

file=open('1.txt', 'r')
cur_copy.copy_expert(sql_copy, file)

--
Alex

In response to

Responses

Browse psycopg by date

  From Date Subject
Next Message Adrian Klaver 2015-10-28 14:55:43 Re: psycopg2 open file for reading
Previous Message Adrian Klaver 2015-10-28 14:35:15 Re: psycopg2 open file for reading