您的位置:首页 > 数据库 > MySQL

scrapy 中文乱码问题解决方案

2014-08-27 00:14 211 查看
http://www.ituring.com.cn/article/114408
最近在学习使用Python操作MySQL数据库,遇到了中文乱码问题。
一、MySQL数据库 库字符集参数:character set = utf8
表字符集参数:charset = utf8
列字符集参数:character set = utf8

二、Python文件 文件编码UTF-8
查看: :set fileencoding 改变: :set fileencoding=utf-8 文件头添加#-*- encoding:utf-8 -*-
创建MySQL数据库连接时加上编码参数,如下例:
conn = MySQLdb.Connection(host='localhost', user='root', passwd='123456', db='test',charset='utf8')
设置Python默认编码(非必须项)
reload(sys)
sys.setdefaultencoding('utf-8')

三、中文处理技巧(Unicode In Python, Completely Demystified) Decode early
Unicode everywhere
Encode late

四、更改mysql字符集: 查看 mysql> SHOW VARIABLES LIKE 'character%' +--------------------------+---------------------------------+
| Variable_name | Value |
+--------------------------+---------------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | utf8 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | D:"mysql-5.0.37"share"charsets" |
+--------------------------+---------------------------------+
修改: To set the default to UTF-8, you want to add the following to my.cnf
[code][client]
[/code]
[code]default-character-set=utf8
[/code]
[code][mysql]
[/code]
[code] default-character-set=utf8
[/code]
[code][mysqld]
[/code]
[code] collation-server = utf8_unicode_ci
[/code]
[code]init-connect='SET NAMES utf8'
[/code]
[code]character-set-server = utf8
[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mysql 乱码 中文 scrapy