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. ...
随机推荐
- ubuntu下pyspark的安装
1.安装jkd1.8(这里不再描述) 2.直接在终端输入pip install pyspark(官网提供的最简单的一种安装方式) 过程如下: Collecting pyspark Downloadin ...
- win10---cmd终端下连接ubantu--SSH SERVER服务
在win10 cmd终端下输入 SSH ubantu@192.168.124.134 之后输入yes 在输入yes
- Oracle账户管理
登录:conn 用户名/口令创建用户create user 用户名 identified by 口令修改密码password 用户名删除用户drop user 用户名如果要删除的用户已经创建表,那么再 ...
- 2.33 定位的坑:class属性有空格
2.33 定位的坑:class属性有空格 前言有些class属性中间有空格,如果直接复制过来定位是会报错的InvalidSelectorException: Message:The given sel ...
- PHP中PSR-[0-4]规范(转)
PHP中PSR-[0-4]规范 更好排版:https://www.zybuluo.com/phper/note/65033 PHP是世界上最伟大的语言,这一点是毋庸置疑的吧.哈哈哈哈哈哈 .这个霸气的 ...
- url的使用
1.urls.py 默认是在主目录中,主路由配置文件,会包含最基本的地址映射,并且每个地址访问都必须要先经过该文件. 作用:通过 urls中定义好的地址找到对应的视图处理函数 urlpatterns ...
- djang 过滤器和装饰器
Django自定义过滤器 Django 自定义过滤器, 在项目目录中建立templatetags目录. 建立my_filter.py 文件, 导入模板模块 :from django import te ...
- HDU 1754:I Hate It(线段树模板)
I Hate It Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- yum源更换为本地光盘
下面来示范一下如何使用光盘文件配置yum库.注意:一定要把/etc/yum.repos.d里的其他文件移到其他地方,否则会报错 进入到/etc/yum.repos.d目录中后创建Yum配置文件: [r ...
- Android USB gadget configfs学习笔记总结
1.一个config_item 是通过显式用户空间mkdir操作创建的,通过rmdir销毁.属性(文件)在mkdir之后出现,可以通过read和write读取或修改属性文件.与sysfs一样,read ...