Chinaunix首页 | 论坛 | 博客
  • 博客访问: 609150
  • 博文数量: 142
  • 博客积分: 116
  • 博客等级: 入伍新兵
  • 技术积分: 1445
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-28 08:37
文章分类

全部博文(142)

文章存档

2017年(7)

2016年(57)

2015年(48)

2014年(30)

我的朋友

分类: Python/Ruby

2015-11-23 19:33:37

被这个错搞了半天,尝试一堆encode,decode,还尝试了
import sys
reload(sys)
sys.setdefaultencoding('utf8')
也不管用。
最后查到需要转成utf-8,再调用sha1.

hashlib.sha1(s).hexdigest()
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-81: 
ordinal not in range(128)

Apparently hashlib.sha1 isn't expecting a unicode object, but rather a sequence of bytes in a strobject. Encoding your unicode string to a sequence of bytes (using, say, the UTF-8 encoding) should fix it:

>>> import hashlib
>>> s = u'é'
>>> hashlib.sha1(s.encode('utf-8'))
阅读(1211) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~