'latin-1' codec can't encode character 的解决方案

来源:互联网 发布:阿里云域名交易平台 编辑:程序博客网 时间:2024/05/22 10:30
分析一个字符串,并更新数据库的时候,出现了如下错误:
'latin-1' codec can't encode character u'\u017e' in position 11: ordinal not in range(256)

进行了一些研究发现,原因是,数据库的编码和数据源的编码不一致,并且包含了不能处理的字符。

有两种方法可用,一个是先预先处理一下字符串,二是设置数据库参数

1. 处理字符串
  1. >>> u= u'hello\u2013world'
  2. >>> u.encode('latin-1','replace') # replace it with a question mark
  3. 'hello?world'
  4. >>> u.encode('latin-1','ignore') # ignore it
  5. 'helloworld'

  6. 或者根据需求进行处理

  7. >>> u.replace(u'\u2013','-').encode('latin-1')
  8. 'hello-world'
  9. If you aren't required to output Latin-1, then UTF-8 is a common and preferred choice. It is recommended by the W3C and nicely encodes all Unicode code points:

  10. >>> u.encode('utf-8')
  11. 'hello\xe2\x80\x93world
2. 设置数据库
  1. db.set_character_set('utf8')
  2. dbc.execute('SET NAMES utf8;')
  3. dbc.execute('SET CHARACTER SET utf8;')
  4. dbc.execute('SET character_set_connection=utf8;')
0 0
原创粉丝点击