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的更多相关文章

  1. Python - TypeError: unicode argument expected, got 'str'

    参考:TypeError: unicode argument expected, got 'str' Python代码: from io import StringIO def main(): f = ...

  2. appium 提示报错“TypeError: 'unicode' object is not callable”的解决方式!

    这里提到的这个报错,是小错误且容易经常会犯,有时需要特别注意使用. 目的要求结果:根据某个元素的id值获取到对应id的text值,并且将获取的text值与本身存在的text值做比较,查看text值是否 ...

  3. Python hashlib Unicode-objects must be encoded before hashing

    Python2中没有这个问题 python3中 hashlib.md5(data)函数中data 参数的类型应该是bytes hash前必须把数据转换成bytes类型 Python 2.7.12 (d ...

  4. hashlib使用时出现: Unicode-objects must be encoded before hashing

    # hashlib.md5(data)函数中,data参数的类型应该是bytes# hash前必须把数据转换成bytes类型>>> from hashlib import md5 F ...

  5. django注册在使用hashlib对密码加密时报Unicode-objects must be encoded before hashing

    在使用sh1等hashlib方法进行加密时报:Unicode-objects must be encoded before hashing 解决办法:对要加密的字符串指定编码格式 解决之前: s1=s ...

  6. 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 ...

  7. python3.3 unicode(encode&decode)

    最近在用python写多语言的一个插件时,涉及到python3.x中的unicode和编码操作,本文就是针对编码问题研究的汇总,目前已开源至github.以下内容来自项目中的README. 1 ASC ...

  8. Python2.7 在使用BSTestRunner.py时报错TypeError: unicode argument expected, got 'str'

    python3往这个库中加入了一些新的内容,使得该库在Python2.7中报错. 解决方法是将导入语句 from io import StringIO as StringIO 更换为: from io ...

  9. 爬虫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. ...

随机推荐

  1. Mybatis学习笔记三

    一.延迟加载 延迟加载即加载延迟了,并不是一次性加载完而是按需加载,感觉应该是针对多表查询而言的,即先查询单表等需要另一张表的信息时再去加载,这样能提高数据库的性能: 需要注意的是,mybatis提供 ...

  2. 五、LCD屏填充纯色

    废话不说,直接上代码: lcd.c #include "lcd.h" static int PEN_COLOR = LCD_RED; /* 定义画笔(前景)颜色 */ static ...

  3. django做redis缓存

    django中应用redis:pip3 install django-redis - 配置 CACHES = { "default": { "BACKEND": ...

  4. python-—计算器

    python-练习—计算器 一.要求传入字符串,计算结果string='1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/ ...

  5. git使用简明教程

    1.自己在gitlab.XXX.com创建一个项目 点击右上角的"+"符号,创建新项目. 项目名:xxxtest 2.在master分支提交一个文件Readme.txt 文件内容: ...

  6. java.lang.OutOfMemoryError: GC overhead limit exceeded

    前端请求:{"code":400,"message":"Handler dispatch failed; nested exception is ja ...

  7. MAC机下用Terminal操作MySql

    在MAC机上安装好MySql后,在Terminal内运行mysql时会提示mysql command not found命令.这是因为没有把运行时的路径添加到$PATH变量中.检查$PATH变量中是否 ...

  8. SQL注入之Sqli-labs系列第九关和第十关(基于时间盲注的注入)

    开始挑战第九关(Blind- Time based- Single Quotes- String)和第十关( Blind- Time based- Double Quotes- String) gog ...

  9. 如何用DAX实现查看每个月中不同类别排名前一位,以及一个简单的svg案例

    现在给大家带来的是如何用DAX实现查看每个月中不同类别的排名前一位,最终完成效果如下!!! 首先我们需要两张简单的表 基数表 和类别表 当我们创建好表之后,我们再创建一个表格,然后我们将类别表里的列值 ...

  10. centos7 firewalld基本使用

    firewalld的基本使用 启动: systemctl start firewalld 查看状态: systemctl status firewalld 停止: systemctl disable ...