python cyrillic decode

后端 未结 2 1754
别那么骄傲
别那么骄傲 2021-01-24 17:54

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=\"XX         


        
2条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-24 18:29

    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.

提交回复
热议问题