escaping data for copy_from()

From: Josh Kupershmidt <schmiddy(at)gmail(dot)com>
To: psycopg(at)postgresql(dot)org
Subject: escaping data for copy_from()
Date: 2011-12-17 20:31:42
Message-ID: CAK3UJRHrrsNjoOm54NYJT_O15Wh-jDcm1332OYraZxVJhN3S6Q@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: psycopg

Hi all,

I haven't seen a built-in way to escape data meant for copy_from().
The example code in examples/copy_from.py uses only pre-escaped data
e.g:

data.write('\n'.join(['Tom\tJenkins\t37',
'Madonna\t\N\t45',
'Federico\tDi Gregorio\t\N']))

which works OK for such pre-escaped data, but what I really want is a
way to escape arbitrary text blobs for use with copy_from() -- for
instance, text containing newlines, tabs, backslash characters, and
other sequences which need escaping.

Is there some easy way to handle this problem I'm missing, or do I
need to just write my own escaping code?

Josh

Browse psycopg by date

  From Date Subject
Next Message Federico Di Gregorio 2011-12-19 10:25:25 RELEASE: psycopg 2.4.4
Previous Message Federico Di Gregorio 2011-12-16 09:23:24 Re: [snafu] isolation-level change in 2.4.2