Python文件設(shè)置編碼 utf-8 (文件前面加上 #encoding=utf-8), 同時(shí)文件保存的格式也應(yīng)該是utf8 MySQL數(shù)據(jù)庫charset=utf-8 ,數(shù)據(jù)庫的編碼必須是utf8 python連接MySQL時(shí)加上編碼參數(shù) conn = MySQLdb.Connection(host='localhost', user='root', passwd='123', db='test',charset='utf8') 設(shè)置Python默認(rèn)編碼(個(gè)人感覺此無太多意義) reload(sys) sys.setdefaultencoding('utf-8') 二種 tmp = str(row[2]) tmp = tmp.decode('utf-8') 我的實(shí)際情況是將連接字符串加上charset=‘utf8’,成功。如下:(當(dāng)然我的python文件設(shè)置編碼為utf-8,mysql的編碼也是utf-8) MySQLdb.Connection(host='localhost', user='root', passwd='123', db='test',charset='utf8') |
|