当前位置:Gxlcms > 数据库问题 > python字符集的转换(mysql数据乱码的处理)

python字符集的转换(mysql数据乱码的处理)

时间:2021-07-01 10:21:17 帮助过:23人阅读

!/usr/bin/env python # _*_ encoding:utf-8 _*_ ‘‘‘ author: tiantiandas ‘‘‘ import sys reload(sys) sys.setdefaultencoding(gbk) import MySQLdb def Connect_Mysql(sql,host): db_info = {host: host, user: test, db: TestDB, passwd: dnstest, charset:gbk} #很关键 try: connect = MySQLdb.connect(**db_info) cursor = connect.cursor() cursor.execute(sql) connect.commit() result = cursor.fetchone() return result except Exception as e: print e sys.exit(10) def main(): 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: print update Connect_Mysql(sql=update,host=host2) except Exception as e: print e except Exception as e: print e if __name__ == __main__: main()
  • 几个关键点:
    • sys.setdefaultencoding(‘gbk‘)  :  这段代码让从A库拉出的数据,python会将其解码为成gbk。(大概是这个意思)
    • mysql编码: charset:gbk  :这个调整让写入到库中的数据字符集为gbk
  • 所以如果拉出的数据是为了自己看的时候,就不需要 sys.setdefaultencoding(‘gbk‘)这段代码了 。
  • 2.关于编码和解码

    • chardet模块 
      • chardet是字符编码识别的模块,使用如下:
      • #!/usr/bin/env python
        # _*_ encoding:utf-8 _*_
        import chardet
          
        a="天天"
        print chardet.detect(a)
        
        结果:
        {confidence: 0.75249999999999995, encoding: utf-8}
      • 如果要对一个大文件进行编码识别,如下的方法,可以提高识别速度:(相比第一种,这种确实会快一些)
      • import urllib
        from chardet.universaldetector import UniversalDetector
        usock = urllib.urlopen(http://www.baidu.com/)
        #创建一个检测对象
        detector = UniversalDetector()
        for line in usock.readlines():
            #分块进行测试,直到达到阈值
            detector.feed(line)
            if detector.done: break
        #关闭检测对象
        detector.close()
        usock.close()
        #输出检测结果
        print detector.result
        
        运行结果:
        {confidence: 0.99, encoding: GB2312}
      • 有了chardet模块,就可以识别获取数据的字符集格式,之后就可以将数据转换为想要的字符集格式了。
    • 两个函数:
      • decode:可以将数据解码为想要的字符集格式
      • encode:可以将数据编码为想要的字符集格式
      • python识别的是unicode,所以是用decode现将数据转换为unicode,之后再用encode将数据转换为想要的字符集。
    • 测试代码:
    • >>> name="天天"
      >>> name 
      \xe5\xa4\xa9\xe5\xa4\xa9  #天天 汉字的gbk码
      
      >>> b=name.decode(gbk)   
      >>> b
      u\u6fb6\u2541\u3049
      
      >>> c=b.encode(utf8)
      >>> c
      \xe6\xbe\xb6\xe2\x95\x81\xe3\x81\x89
      
      ——————————————————————————
      
      >>> \xcc\xec\xcc\xec.decode(gbk)
      u\u5929\u5929
      >>> \xcc\xec\xcc\xec.decode(gbk).encode(utf8)
      \xe5\xa4\xa9\xe5\xa4\xa9
      >>> 天天
      \xe5\xa4\xa9\xe5\xa4\xa9

      技术分享

     

    python字符集的转换(mysql数据乱码的处理)

    标签:乱码问题   passwd   数据转换   try   提高   pen   bsp   pre   admin   

    人气教程排行