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

本文参考:http://blog.csdn.net/crazyhacking/article/details/39375535

chardet模块:http://blog.csdn.net/tianzhu123/article/details/8187470

字符集转换部分:http://blog.chinaunix.net/uid-26249349-id-2846894.html

python 转码 mysql 字符集

两个msyql库,字符集均为gbk,需要从A库中取数据,插入到B库中,其中某些字段值为中文。

代码

#!/usr/bin/env python
# _*_ encoding:utf- _*_ '''
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() def main():
domain = sys.argv[]
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')这段代码了 。

chardet 模块

chardet是字符编码识别的模块,使用如下:

#!/usr/bin/env python
# _*_ encoding:utf- _*_
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'
上一篇:深度剖析Linux与Windows系统的区别,新手必读!


下一篇:关于 Senparc.Weixin.Cache.Redis 引用的 StackExchange.Redis 版本不匹配的反馈测试