1

I recieved a list of tuples from a mySQL database.
When I try to print an item, here is the result:

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

This is cp1251, according to https://2cyr.com/decode/?lang=ru

I have tried lots of variations of .encode().decode() with errors='ignore' params but without any success. Any ideas?

UPD I recieve my list of tuples with mysql-connector-python.

z is the list. The result above is from 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 Here is a wierd decoder. Hope it will help smb.

I realised that the problem is in inserting to my DB. Will dig here.

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
Snobby
  • 1,067
  • 3
  • 18
  • 38
  • Please show the code which produced that. – wallyk Aug 20 '16 at 20:46
  • 1
    I'm not so sure about your conclusion "this is CP1251". Using that same web page, I can view it in a variety of encodings, but none of them result in a complete readable "Russian" text. – Jongware Aug 20 '16 at 22:18
  • I don't know how is it happening. But after copying this string here, it's changing somehow, so I can't use it on this website. Very stange – Snobby Aug 21 '16 at 07:26
  • Are you sure your database is OK? MySQL has some encoding-related issues. I didn't use MySQL since I quit PHP, but as far as I remember, you had to be sure that your database encoding and your connection encoding are the same. – Ihor Pomaranskyy Aug 21 '16 at 08:24
  • Difficult question, don't know how to check this. When I am logging in my database I see this values in an appropriate format – Snobby Aug 21 '16 at 08:29
  • We don't know what you've tried, and how they failed. – Karoly Horvath Aug 21 '16 at 08:33
  • so you think there is no point of trying to decode it? I want to deal somehow with the existing result, hoped that's a shortest way, if not, will try to do smth with connector&DB – Snobby Aug 21 '16 at 09:15

1 Answers1

0

The problem was in database. The sting was already damaged during the insertion. I tried mysql_set_charset('utf8'); in my insertion script and everything went allright.

Snobby
  • 1,067
  • 3
  • 18
  • 38