python3 TypeError: Unicode-objects must be encoded before hashing
python3下,利用hash值对字符串进行md5加密时报错:
TypeError: Unicode-objects must be encoded before hashing
原因是:
python3跟python2区别:python3下字符串为Unicode类型,而hash传递时需要的是utf-8类型,因此,需要类型转换
调用函数时,将字符串进行类型转换
import hashlib
def get_md5(s):
m = hashlib.md5()
m.update(s)
return m.hexdigest()
print(get_md5("gg".encode("utf8")))
或者
def get_md5(s):
m = hashlib.md5()
m.update(str(s).encode('utf-8'))
return m.hexdigest()
print(get_md5("gg"))
done!
python3 TypeError: Unicode-objects must be encoded before hashing的更多相关文章
- Python - TypeError: unicode argument expected, got 'str'
参考:TypeError: unicode argument expected, got 'str' Python代码: from io import StringIO def main(): f = ...
- appium 提示报错“TypeError: 'unicode' object is not callable”的解决方式!
这里提到的这个报错,是小错误且容易经常会犯,有时需要特别注意使用. 目的要求结果:根据某个元素的id值获取到对应id的text值,并且将获取的text值与本身存在的text值做比较,查看text值是否 ...
- Python hashlib Unicode-objects must be encoded before hashing
Python2中没有这个问题 python3中 hashlib.md5(data)函数中data 参数的类型应该是bytes hash前必须把数据转换成bytes类型 Python 2.7.12 (d ...
- hashlib使用时出现: Unicode-objects must be encoded before hashing
# hashlib.md5(data)函数中,data参数的类型应该是bytes# hash前必须把数据转换成bytes类型>>> from hashlib import md5 F ...
- django注册在使用hashlib对密码加密时报Unicode-objects must be encoded before hashing
在使用sh1等hashlib方法进行加密时报:Unicode-objects must be encoded before hashing 解决办法:对要加密的字符串指定编码格式 解决之前: s1=s ...
- python3 TypeError: a bytes-like object is required, not 'str'
在学习<Python web开发学习实录>时, 例11-1: # !/usr/bin/env python # coding=utf-8 import socket sock = sock ...
- python3.3 unicode(encode&decode)
最近在用python写多语言的一个插件时,涉及到python3.x中的unicode和编码操作,本文就是针对编码问题研究的汇总,目前已开源至github.以下内容来自项目中的README. 1 ASC ...
- Python2.7 在使用BSTestRunner.py时报错TypeError: unicode argument expected, got 'str'
python3往这个库中加入了一些新的内容,使得该库在Python2.7中报错. 解决方法是将导入语句 from io import StringIO as StringIO 更换为: from io ...
- 爬虫python3:TypeError: cannot use a string pattern on a bytes-like object
import re from common_p3 import download def crawl_sitemap(url): sitemap = download(url) links = re. ...
随机推荐
- vue-router + ElementUI实现NavMenu 导航菜单 选中状态的切换
elemen-ui官方网站:http://element.eleme.io/#/zh-CN/component/menu 新手小白利用vue+element-ui尝试搭建后台管理系统, 效果是这样的, ...
- <顺序访问><随机访问><HDFS>
Overview 如果你了解过HDFS,至少看过这句话吧: HDFS is a filesystem designed for storing very large files with stream ...
- Oracle create tablespace 、create user and so on
1.创建临时表空间 CREATE TEMPORARY TABLESPACE test_tempTEMPFILE 'C:\oracle\product\10.1.0\oradata\orcl\test_ ...
- django做redis缓存
django中应用redis:pip3 install django-redis - 配置 CACHES = { "default": { "BACKEND": ...
- web-view中下载微信头像跨域解决方案
let img = new Image() // 头像地址后边添加时间戳可解决跨域问题 555. img.src = 'http://wx.qlogo.cn/mmopen/vi_32/RnLIHfXi ...
- prototype和_proto_
__proto__(隐式原型)与prototype(显式原型) 显式原型 explicit prototype property:用来实现基于原型的继承与属性的共享. 每一个函数在创建之后都会拥有一个 ...
- 河南省第四届ACM省赛(T3) 表达式求值
表达式求值 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Dr.Kong设计的机器人卡多掌握了加减法运算以后,最近又学会了一些简单的函数求值,比如,它知道函数min ...
- ubuntu + samba 共享失败
主机版本:ubuntu14.04 问题描述:Samba服务配置后仍然不通. samba服务这么成熟的东西,本以为在ubuntu上修改下smb.conf重启就得了,但是访问还是失败. 表现是,可以看到s ...
- Windows下安装Python模块时环境配置
“Win +R”打开cmd终端,如果直接在里面使用pip命令的时候,要么出现“syntax invalid”,要么出现: 'pip' is not recognized as an internal ...
- 打开网页直接弹出qq对话框?
代码一: http://wpa.qq.com/msgrd?v=3&uin=此处输入QQ号&site=qq&menu=yes 代码二: <iframe src=" ...