I have a problem with comparing a UTF-8 string obtained from PostgreSQL database:
>>> db_conn = psycopg2.connect("dbname='foo' user='foo' host='localhost' password='xxx'")
>>> db_cursor = db_conn.cursor()
>>> sql_com = ("""SELECT my_text FROM table WHERE id = 1""")
>>> db_cursor.execute(sql_com)
>>> sql_result = db_cursor.fetchone()
>>> db_conn.commit()
>>> db_conn开发者_如何学运维.close()
>>> a = sql_result[0]
>>> a
u'M\xfcnchen'
>>> type(a)
<type 'unicode'>
>>> print a
München
>>> b = u'München'
>>> type(b)
<type 'unicode'>
>>> print b
München
>>> a == b
False
I am really confused why is this so, I can someone tell me how should I compare a string with an Umlaut from the database to another string, so the comparison is true? My database is UTF8:
postgres@localhost:$ psql -l
List of databases
Name | Owner | Encoding
-----------+----------+----------
foo | foo | UTF8
This is clearly a problem with locale of your console.
u"München"
is u'M\xfcnchen'
in Unicode and 'M\xc3\xbcnchen'
in UTF-8. That latter is your München
if taken as ISO8859-1 or CP1252.
Psycopg2 seems to supply you with correct Unicode values, as it should.
If you type
b = 'München'
What do you get from type(b) ??
Maybe you don't need to literally transform the string into unicode text as Python will automatically note this.
EDIT: I get this from my python CLI:
>>> b = u'München'
>>> b
u'M\xfcnchen'
>>> print b
München
While you are gettin' your print result in a different encoding
精彩评论