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. 20165214 实验三 敏捷开发与XP实践

    一.实验报告封面 课程:Java程序设计 班级:1652班 姓名:朱文远 学号:20165214 指导教师:娄嘉鹏 实验日期:2018年4月28日 实验时间:15:35 - 17:15 实验序号:三 ...

  2. ESP8266 上报数据到贝壳物联

    下面贴上代码: #include <aJSON.h> #include <SPI.h> #include <Wire.h> #include <dht11.h ...

  3. vue中七牛插件使用

    <template> <div id="cxUpload" class="cx-upload"> <button id=" ...

  4. angular把echarts封装为指令(配合requirejs)

    1.在require中配置echartsjs文件 2.在directives下定义指令(定义为全局的指令,任何页面调用都可以) define(['app','echarts'],function(ap ...

  5. 微信小程序中时间转化为时间戳(安卓和苹果兼容性)

    在IOS中时间显示NAN,后来才知道是由于安卓和IOS时间解析时间的时间格式不一致: 在安卓机中2018-06-21 16:00:21 IOS识别的格式是 2018/06/21: 所以在处理IOS机器 ...

  6. maven 细节 —— scope、坐标

    对于 idea 开发环境,测试代码便是在 src/test/java(该java目录会在创建时标注为测试文件夹) 目录下的 .java 代码为测试代码: 1. scope scope的分类 compi ...

  7. Python学习笔记第六周

    目录 一.基础概念 面向对象编程 1.面向对象的几个核心特点 1.class类 2.object对象 3.encapsulation封装 4.inheritance继承 5.polymorphism多 ...

  8. systemd设置静态IP

    /********************************************************************************* * systemd设置静态IP * ...

  9. Linux用管道命令对文件的移动

    我的问题是这样的:我有一个文件夹,里面有大约有1000个文件,然后我想把这样的一部分文件给随机分成两部分,一部分含有100张,另外一部分含有剩下的所有的文件,这个时候如果是在Linux图形界面的话直接 ...

  10. fckeditor配置

    <!DOCTYPE html> <html > <head> <title>发布</title> <meta name="v ...