Python:解码俄语字符串

时间:2016-08-20 20:44:07

标签: python decode cp1251

我从mySQL数据库中收到了一个元组列表 当我尝试打印项目时,结果如下:

Далоев ÐлекÑандр
<class 'str'>

根据https://2cyr.com/decode/?lang=ru

,这是cp1251

我尝试了.encode().decode()errors='ignore'参数的许多变体但没有任何成功。有什么想法吗?

UPD 我收到了mysql-connector-python的元组列表。

z是列表。上面的结果来自z[0][0]

def select_name(add):
z = []
try:
    dbconfig = read_db_config()
    conn = MySQLConnection(**dbconfig)
    cursor = conn.cursor()
    cursor.execute("select name from phone_add where ph_add = " + str(add) + ";")

    row = cursor.fetchone()
    while row is not None:
        z.append(row)
        row = cursor.fetchone()
    return z

except Error as e:
    print(e)

finally:
    cursor.close()
    conn.close()

UPD2 这是一个奇怪的解码器。 希望它会有所帮助。

我意识到问题在于插入我的数据库。会在这里挖掘。

q = string

codings = ['latin1', 'utf8', 'cp1251', 'unicode-escape', 'cp866']
exceptions = ['ignore', 'strict', 'xmlcharrefreplace', 'backslashreplace']
for i in codings:
    for j in codings:
        for z in exceptions:
            for p in exceptions:
                try:
                    print(q.encode(i, errors=z).decode(j, errors=p) + '<------' + i + ' ' + j + ' ' + z + ' ' + p)
                except:
                    pass

1 个答案:

答案 0 :(得分:0)

问题出在数据库中。 插入期间已经损坏了刺痛。 我在插入脚本中尝试了mysql_set_charset('utf8');,一切都很顺利。

相关问题