python cyrillic decode

故事扮演 提交于 2019-12-20 04:46:24

问题


I'm trying to print cyrillic chars selected from mysql. Here is my code: content id DB is cp1251

>>> db = MySQLdb.connect(host="localhost", user="XXX", passwd="XXXX" )
>>> cursor = db.cursor()
>>> cursor.execute("""select id,title,cat,text,tags,date from db1.table1;""")
>>> test=cursor.fetchone()
>>> somevar=test[1]
>>> somevar=somevar.decode('utf8')
>>> print somevar
Result: ?????? ?? ????????

Please guide me how to print this correctly. Thx.


回答1:


This helped me (got it from here):

db = MySQLdb.connect("localhost", config.db_user, config.db_pwd, config.db_name)

# here's the magic
db.set_character_set("utf8")
dbc = db.cursor()
dbc.execute("SET NAMES utf8;")
dbc.execute("SET CHARACTER SET utf8;")
dbc.execute("SET character_set_connection=utf8;")

# and here goes your SELECT for cyrillic fields
dbc.execute("SELECT id, title, cat, text, tags, date FROM db1.table1;")

# and then you just get the results
test = dbc.fetchone()
somevar = test[1]
print somevar



回答2:


try this:

somevar = somevar.decode('cp1251')

If that does not help, try to add charset='cp1251' parameter in MySQLdb.connect and there is use_unicode parameter, maybe you should use it to...


all connect parameter you can find here https://github.com/farcepest/MySQLdb1/blob/master/MySQLdb/connections.py

use_unicode

If True, text-like columns are returned as unicode objects using the connection's character set. Otherwise, text-like columns are returned as strings. columns are returned as normal strings. Unicode objects will always be encoded to the connection's character set regardless of this setting.

charset

If supplied, the connection character set will be changed to this character set (MySQL-4.1 and newer). This implies use_unicode=True.



来源:https://stackoverflow.com/questions/15907509/python-cyrillic-decode

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!