TypeError: argument 1 must be a string or unicode

2019-07-10 04:01发布

问题:

Using this query:

04/25/2017 00:42:28.180 INFO (u"UPDATE posts SET translated_text='Unroll.me CEO dice que es "desgarrador" que los usuarios se molesten Unroll.me vendi\xf3 sus datos de correo electr\xf3nico anonimizado a Uber', detected_language='en' WHERE post_id=2", u'Unroll.me CEO dice que es "desgarrador" que los usuarios se molesten Unroll.me vendi\xf3 sus datos de correo electr\xf3nico anonimizado a Uber')

With psycopg2, when I use: cur.execute(query) I get:

TypeError: argument 1 must be a string or unicode object

What is the best option to use query and passing a unicode value. Currently Im already parametrizing the SQL query and passing the 'u'.

return u"UPDATE posts SET translated_text='%s', detected_language='%s' WHERE post_id=%s;" % (
        translation, detected_language['language'], str(post_id))

I added also without making a difference:

import psycopg2.extensions

psycopg2.extensions.register_type(psycopg2.extensions.UNICODE)
psycopg2.extensions.register_type(psycopg2.extensions.UNICODEARRAY)

回答1:

That log is showing a tuple of unicode strings, rather than a unicode string. Try logging the type of query before you execute it to check.

The preferred way to log parameterised query is:

query = u"UPDATE posts SET translated_text='%s', detected_language='%s' WHERE post_id=%s;"
vars = translation, detected_language['language'], str(post_id) # tuple
cur.execute(query, vars)

You may also wish to explicitly convert the strings of vars into unicode rather than relying on this to be done implicitly. eg.

vars = translation, detected_language['language'].decode("utf8"), str(post_id).decode("utf8")