python西里尔文解码 [英] python cyrillic decode
本文介绍了python西里尔文解码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试打印从mysql中选择的西里尔字符。这是我的代码:
content id DB是cp1251
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.
推荐答案
这对我有帮助(从此处):
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
这篇关于python西里尔文解码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文