- This article references: http://blog.csdn.net/crazyhacking/article/details/39375535 thank you for finishing!
- Chardet module: http://blog.csdn.net/tianzhu123/article/details/8187470
- Character Set conversion section: http://blog.chinaunix.net/uid-26249349-id-2846894.html
1.mysql garbled problem:
- Background: Two MSYQL libraries, character sets are GBK, need to fetch data from a library, insert into B library, some of the field values are Chinese.
- Code:
#!/usr/bin/env python#_*_ encoding:utf-8 _*_" "Author:tiantiandas" "Importsysreload (SYS) sys.setdefaultencoding ('GBK')ImportMySQLdbdefConnect_mysql (sql,host): Db_info= {'Host': Host,'User':'Test', 'DB':'TestDB', 'passwd':'dnstest', 'CharSet':'GBK'}#is critical Try: Connect= MySQLdb.connect (* *db_info) Cursor=connect.cursor () cursor.execute (SQL) connect.commit () result=Cursor.fetchone ()returnresultexceptException as E:Printe Sys.exit (10)defmain (): Domain= Sys.argv[1] Query='Select Name,admindesc from Emailbox where domain= "{0}"'. Format (domain)Try: Name, Admindesc= Connect_mysql (sql=query,host="host1") Update="update Emailbox set name= ' {0} ', admindesc= ' {1} where domain= ' {2} '". Format (NAME,ADMINDESC)Try: Printupdate connect_mysql (SQL=update,host='Host2') exceptException as E:PrinteexceptException as E:Printeif __name__=='__main__': Main ()
- Several key points:
- Sys.setdefaultencoding (' GBK '): This code allows data to be pulled from the a library, and Python decodes it into GBK. (That's probably what it means)
- MySQL code: CHARSET:GBK: This adjustment allows the data set written to the library to be GBK
- So if you pull out the data for your own viewing, you don't need the sys.setdefaultencoding (' GBK ') code.
2. About encoding and decoding
Python character set conversion (MySQL data garbled processing)