python – hashlib.md5()TypeError:Unicode对象必须在散列之前进行编码
发布时间:2021-03-30 14:12:19 所属栏目:Python 来源:互联网
导读:我是新的编码,并遇到一个问题,试图编码一个字符串. import hashlib a = hashlib.md5() a.update(hi)Traceback (most recent call last): File pyshell#22, line 1, in module a.update(hi)TypeError: Unicod
我是新的编码,并遇到一个问题,试图编码一个字符串. >>> import hashlib >>> a = hashlib.md5() >>> a.update('hi') Traceback (most recent call last): File "<pyshell#22>",line 1,in <module> a.update('hi') TypeError: Unicode-objects must be encoded before hashing >>> a.digest() b'xd4x1dx8cxd9x8fx00xb2x04xe9x80tx98xecxf8B~' 是(a)现在被认为是编码? 第二个问题:当我在脚本中运行相同的代码时,我得到这个错误: import hashlib a = hashlib.md5() a.update('hi') a.digest() 追溯(最近呼叫最后): 为什么代码在shell中工作而不是脚本? 谢谢. 解决方法因为你编码简单的字符串,我推断你正在运行Python 3,其中所有的字符串都是unicode对象,你有两个选择:>提供字符串的编码,例如:“没有人检查”.encode(‘utf-8’) m.update(b"Nobody inspects") m.update(b" the spammish repetition") shell脚本中行为不同的原因是脚本停止在错误上,而在shell中,最后一行是单独的命令,但是由于上一个错误,仍然没有执行任何操作. (编辑:十堰站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- Python不捕捉MemoryError
- python – 使用BeautifulSoup在关闭body之前插入元素
- python – 将数字字符引用表示法转换为unicode字符串
- 在python中使用ElementTree将xml元素作为第一个子元素插入
- Python:Tkinter小部件背景(按钮,条目等)
- 可选的python参数没有破折号但有附加参数?
- python – django collectstatic overriding
- Python:如何在字符串中剪切超过2个相等字符的序列
- python – django:django-tables2 DetailView CBV不会显示
- python – 了解matplotlib xticks语法