简体   繁体   English

如何在Python中正确比较psycopg2中的unicode字符串?

[英]how to correctly compare unicode string from psycopg2 in Python?

I have a problem with comparing a UTF-8 string obtained from PostgreSQL database: 我在比较从PostgreSQL数据库获得的UTF-8字符串时遇到问题:

>>> 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: 我的数据库是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. u"München"u'M\\xfcnchen'在Unicode和'M\\xc3\\xbcnchen'在UTF-8。 That latter is your München if taken as ISO8859-1 or CP1252. 后者是你的München如果被视为ISO8859-1或CP1252。

Psycopg2 seems to supply you with correct Unicode values, as it should. Psycopg2似乎为您提供了正确的Unicode值。

If you type 如果你输入

b = 'München'

What do you get from type(b) ?? 你从类型(b)得到什么?

Maybe you don't need to literally transform the string into unicode text as Python will automatically note this. 也许您不需要将字符串逐字转换为unicode文本,因为Python会自动记录这一点。

EDIT: I get this from my python CLI: 编辑:我从我的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 虽然您以不同的编码获得打印结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM