Re: psycopg2 open file for reading

From: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>
To: Dan Sawyer <dansawyer(at)earthlink(dot)net>, psycopg(at)postgresql(dot)org
Subject: Re: psycopg2 open file for reading
Date: 2015-10-28 15:28:37
Message-ID: 5630E9A5.2010409@aklaver.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: psycopg

On 10/28/2015 08:25 AM, Dan Sawyer wrote:
> Yes. That is what is producing the errors. The pertinent lines are:
>
> conn = psycopg2.connect(conn_string)
> cursoro = conn.cursor(cursor_factory=psycopg2.extras.DictCursor)
> ... processing python code
> ... creates test.txt
> ... closes test.txt
> f = open("/tmp/test.txt","r")
> copy_string = 'copy temp_tbl from '/tmp/test.txt' csv delimiter
> '|';'
> cursoro.copy_expert(copy_string, file)
>
> Is a cursor the wrong way to reference copy_expert ?

No, but this:

'copy temp_tbl from '/tmp/test.txt'

needs to be:

'copy temp_tbl from FROM STDIN'

See:

http://initd.org/psycopg/docs/cursor.html#cursor.copy_expert
>
> On 10/28/2015 08:06 AM, Shulgin, Oleksandr wrote:
>> On Wed, Oct 28, 2015 at 4:04 PM, Dan Sawyer <dansawyer(at)earthlink(dot)net
>> <mailto:dansawyer(at)earthlink(dot)net>> wrote:
>>
>> The file open command is:
>>
>> f = open("/tmp/test.txt","r")
>>
>> Is this correct? Is the read method syntax correct
>>
>>
>> Well, it looks correct. Do you get any errors with that?
>>
>> On 10/28/2015 07:42 AM, Shulgin, Oleksandr wrote:
>>> On Wed, Oct 28, 2015 at 3:35 PM, Adrian Klaver
>>> <adrian(dot)klaver(at)aklaver(dot)com <mailto: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
>>
>>
>

--
Adrian Klaver
adrian(dot)klaver(at)aklaver(dot)com

In response to

Responses

Browse psycopg by date

  From Date Subject
Next Message Adrian Klaver 2015-10-28 17:04:24 Re: psycopg2 open file for reading
Previous Message Dan Sawyer 2015-10-28 15:25:06 Re: psycopg2 open file for reading