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. Unity3d代码及效率优化总结

    1.PC平台的话保持场景中显示的顶点数少于200K~3M,移动设备的话少于10W,一切取决于你的目标GPU与CPU. 2.如果你用U3D自带的SHADER,在表现不差的情况下选择Mobile或Unli ...

  2. adb shell模拟点击事件(input tap)

    前言:appium定位也不是万能的,有些元素还是定位不到,这个时候只能换一个方式定位了,可以使用这个adb shell模拟点击. 1.input可以实现的功能 输入文本信息:input text gu ...

  3. win7 java环境变量配置

    进行win7下Java环境变量配置      在"系统变量"下进行如下配置: (1)新建->变量名:JAVA_HOME变量值 C:\Program Files\Java\jd ...

  4. ob_gzhandler — ob_start callback function to gzip output buffer

    <?php ob_start("ob_gzhandler"); ?><html><body><p>This should be a  ...

  5. python 正则进阶常用方法

    表达式 描述 正则表达式示例 符号 literal 匹配文本字符串的字面值literal foo rel1|rel2 匹配正则表达式rel1或rel2 foo|bar . 匹配任何字符(除了\n之外) ...

  6. 设置idea文件类型

  7. [LeetCode&Python] Problem 717. 1-bit and 2-bit Characters

    We have two special characters. The first character can be represented by one bit 0. The second char ...

  8. JSON JAVA 总结

    1.如下是我所用json第三方jar包的maven坐标 <!--可引用的jar--> <dependency> <groupId>net.sf.json-lib&l ...

  9. input标签(图像域和隐藏域)

    图像域(图像提交按钮): <input type="image" name="..." src="imageurl"  /> 隐 ...

  10. codefoce Cooking Time

    #include <bits/stdc++.h> using namespace std; struct T { // 贪心 优先弹出相邻靠后的材料 int id; int p; bool ...