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. ...
随机推荐
- Appium Desktop 介绍及使用
一.AppiumDesktop介绍 1.Appium-server的图形界面.可以设置选项.启动/停止服务器.查看日志等功能:且无须提前安装Node / NPM,因为Node运行时直接与Appium ...
- mysql创建用户并给用户分配权限
1.登录Mysql [root@xufeng Desktop]# mysql -u root -pEnter password: Welcome to the MySQL monitor. Comma ...
- Python 区分方法和函数
def func(): print("我是函数") class Foo: def chi(self): print("我是吃") # print(func) # ...
- ES6 class的基本语法-学习笔记
1.基本上,ES6 的class可以看作只是一个语法糖,它的绝大部分功能,ES5 都可以做到,新的class写法只是让对象原型的写法更加清晰.更像面向对象编程的语法而已. 类的内部所有定义的方法,都是 ...
- [转]Ubuntu安装Python3.6
Ubuntu安装Python3.6 Ubuntu默认安装了Python2.7和3.5 输入命令python
- str 类型
1.capitalize():首字母大写 2.center(size,fillwith): 3.count(sub,start,end):计算子序列的个数 4.decode() 5.encode() ...
- scala quick check
Scala 特性 面向对象特性 函数式编程 Scala也是一种函数式语言,其函数也能当成值来使用.Scala提供了轻量级的语法用以定义匿名函数,支持高阶函数,允许嵌套多层函数,并支持柯里化.Sca ...
- JavaBasic_09
方法的参数传递 方法调用时参数值的传递可以分为"值传递"和"引用传递"两种 值传递 - a.当方法的参数为基本数据类型时 b.实参的值被复制给形参,改变形参不会 ...
- POJO,JaveBean,VO,DTO
POJO - POJO(Plain Ordinary Java Object)简单的Java对象,实际就是普通JavaBeans,是为了避免和EJB混淆所创造的简称. 使用POJO名称是为了避免和EJ ...
- nginx根据url中的参数进行转发
在实际项目中,由于https安全策略,我们无法直接跳转到我们想要跳转到的地址 例如 url:https://abc.dc.com/image?url=https://vpic.video.qq.com ...