Getting adaped output from unicode text, without a connection

Поиск
Список
Период
Сортировка
Искать

Getting adaped output from unicode text, without a connection

От:
"Hefferon, James S." <jhefferon@smcvt.edu>
Дата:
Hello,

I need to write some static .sql files (just as an explanation, it is startup code for Django).  Some of the strings are non-ascii.  I'm stumped about how to get utf-8 instead of latin-1 in the file.
 
I'm generating the .sql as strings, without a connection.  I found a prior message on this list that suggested what to do (1) and came up with this routine.

def todb(s):
    psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
    psycopg2.extensions.register_type(psycopg2.extensions.DECIMAL)
    psycopg2.extensions.register_type(psycopg2.extensions.FLOAT)
    adapted = psycopg2.extensions.adapt(s)
    return adapted.getquoted()
 
But it encodes in latin-1.  I understand from the docs that I need to get a connection and set the .encoding attribute, but I can't see how to get a connection instance without a db handy.  For instance, 

>>> conn = psycopg2.connect(database = None)

fails.

I expect that I am asking a dopey question (I'm not very sure what I am doing) but I'd appreciate a pointer even if it makes me feel dumb.

Thanks,
Jim

(1) http://www.postgresql.org/message-id/AANLkTik+tq8hbnCj3ROmOLNV1DSw48o4sDoSvnxTN3R8@mail.gmail.com

Re: Getting adaped output from unicode text, without a connection

От:
Daniele Varrazzo <daniele.varrazzo@gmail.com>
Дата:
On Sun, Jul 21, 2013 at 7:56 PM, Hefferon, James S.  wrote:
> Hello,
>
> I need to write some static .sql files (just as an explanation, it is startup code for Django).  Some of the strings are non-ascii.  I'm stumped about how to get utf-8 instead of latin-1 in the file.
>
> I'm generating the .sql as strings, without a connection.  I found a prior message on this list that suggested what to do (1) and came up with this routine.
>
> def todb(s):
>     psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
>     psycopg2.extensions.register_type(psycopg2.extensions.DECIMAL)
>     psycopg2.extensions.register_type(psycopg2.extensions.FLOAT)
>     adapted = psycopg2.extensions.adapt(s)
>     return adapted.getquoted()
>
> But it encodes in latin-1.  I understand from the docs that I need to get a connection and set the .encoding attribute, but I can't see how to get a connection instance without a db handy.  For instance,
>
>>>> conn = psycopg2.connect(database = None)
>
> fails.
>
> I expect that I am asking a dopey question (I'm not very sure what I am doing) but I'd appreciate a pointer even if it makes me feel dumb.

No, it's not a dumb question. But I'm afraid you cannot create a
psycopg connection object without a real database connection.

If you had a working connection you could adapt the string with:

    conn.set_client_encoding('utf8')
    adapted = psycopg2.extensions.adapt(s)
    adapted.prepare(conn)
    return adapted.getquoted()

you don't need the register_type calls above: they only control how
data from the database is converted to Python.

-- Daniele

Re: Getting adaped output from unicode text, without a connection

От:
Adrian Klaver <adrian.klaver@gmail.com>
Дата:
On 07/21/2013 11:56 AM, Hefferon, James S. wrote:
> Hello,
>
> I need to write some static .sql files (just as an explanation, it is startup code for Django).  Some of the strings are non-ascii.  I'm stumped about how to get utf-8 instead of latin-1 in the file.
>
> I'm generating the .sql as strings, without a connection.  I found a prior message on this list that suggested what to do (1) and came up with this routine.
>

>
> I expect that I am asking a dopey question (I'm not very sure what I am doing) but I'd appreciate a pointer even if it makes me feel dumb.

My question would be, why invoke psycopg2 at all?
Can you not just write out the strings?

>
> Thanks,
> Jim
>
> (1) http://www.postgresql.org/message-id/AANLkTik+tq8hbnCj3ROmOLNV1DSw48o4sDoSvnxTN3R8@mail.gmail.com
>


-- 
Adrian Klaver
adrian.klaver@gmail.com

Re: Getting adaped output from unicode text, without a connection

От:
Christophe Pettus <xof@thebuild.com>
Дата:

On Jul 21, 2013, at 1:39 PM, Adrian Klaver wrote:

> My question would be, why invoke psycopg2 at all?
> Can you not just write out the strings?

I assume the OP wanted to do pre-quoting on the strings.

--
-- Christophe Pettus
   xof@thebuild.com


Re: Getting adaped output from unicode text, without a connection

От:
"Hefferon, James S." <jhefferon@smcvt.edu>
Дата:
> I assume the OP wanted to do pre-quoting on the strings.

Exactly so, thank you.  I didn't know the term.

Jim

--------------------------------------
`Does not the Captain seek your advice, sir?'
`Not always,' said Stephen.


FAQ