前端时间从网上爬取了14000+的育儿知识,尝试入库mysql的时候报如下的错误:
"UnicodeEncodeError:'latin-1' codec can't encode character ..."
其中,入库的python脚本已将默认编码设置为utf-8
- default_encoding = 'utf-8'
- reload(sys)
- sys.setdefaultencoding(default_encoding)
网上给出的原因为,mysqldb的默认编码为latin-1,而有些utf-8编码的字符无法通过latin-1 编码,为此改变mysqldb的默认编码为utf-8:
- db.set_character_set('utf8')
- dbc.execute('SET NAMES utf8;')
- dbc.execute('SET CHARACTER SET utf8;')
- dbc.execute('SET character_set_connection=utf8;')