Confused about encoding issue when read from mysql via python code

时间:2016-07-11 22:01:55

标签: python mysql encoding character-encoding

There is one row in Mysql table as following:

1000, Intel® Rapid Storage Technology

The table's charset='utf8' when was created. When I used python code to read it, it become the following:

Intel® Management Engine Firmware

My python code as following:

db = MySQLdb.connect(db,user,passwd,dbName,port,charset='utf8')

The weird thing was that when I removed the charset='utf8' as following: db = MySQLdb.connect(db,user,passwd,dbName,port), the result become correct.

Why when I indicated charset='utf8' in my code, but got wrong result please?

2 个答案:

答案 0 :(得分:0)

Have you tried leaving off the charset in the connect string and then setting afterwards?

db = MySQLdb.connect(db,user,passwd,dbName,port)
db.set_character_set('utf8')

答案 1 :(得分:0)

尝试使用utf8 / utf8mb4时,如果看到 Mojibake ,请检查以下内容。 此讨论也适用于双重编码,这不一定是可见的。

  • 要存储的字节需要utf8编码。
  • INSERTingSELECTing文字需要指定utf8或utf8mb4时的连接。
  • 该列需要声明为CHARACTER SET utf8(或utf8mb4)。
  • HTML应以<meta charset=UTF-8>开头。

另见Python notes