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. ...
随机推荐
- MVC4中使用Uploadify3.2
你使用过 GMail 中附件上传吗?带有上传进度,可以取消正在进行的上传,使用 Uploadify 插件,你也可以做到. Uploadify 是 JQuery 一个著名的上传插件,利用 Flash 技 ...
- Oracle包和包体
一.什么要使用包? 在一个大型项目中,可能有很多模块,而每个模块又有自己的过程.函数等.而这些过程.函数默认是放在一起的(如在PL/SQL中,过程默认都是放在一起 的,即Procedures中),这些 ...
- <Spark><Running on a Cluster>
Introduction 之前学习的时候都是通过使用spark-shell或者是在local模式运行spark 这边我们首先介绍Spark分布式应用的架构,然后讨论在分布式clusters中运行Spa ...
- synchronized(六)
package com.bjsxt.base.sync006;/** * 锁对象的改变问题 * @author alienware * */public class ChangeLock { priv ...
- day 67 django 之ORM 基础安装
一 ORM的基础部分 1 ORM的概念 对象关系映射(Object Relational Mapping(映射),简称ORM)模式是一种为了解决面向对象与关系数据库存在的互不匹配的现象的技术. 2 ...
- Linux:http配置
http配置 1.挂载系统:mount /dev/cdrom /mnt/cdrom2.安装:rpm -ivh httpd-2.4.6-88.el7.centos.x86_64.rpm2.启动服务:sy ...
- centos的mysql升级之后密码重置
1.配置文件添加过滤密码选项 #vim /etc/my.cnf 跳过密码校验 2.重启mysql服务 #/etc/init.d/mysqld restart 3.#mysql -uroot -p ...
- session_id 生成原理
PHPSESSID生成 生成规则是根据hash_func散列来生成的,相关的参数有: - 客户端IP - 当前时间(秒) - 当前时间(微妙) - PHP自带的随机数生产器 hash_func是php ...
- Ubuntu16.04 执行sudo apt-get update出现E: Sub-process returned an error code错误
Reading package lists... Done E: Problem executing scripts APT::Update::Post-Invoke-Success 'if /usr ...
- JAVA拼合数组方法
方法一: package org.ken.array; import java.lang.reflect.Array; import java.util.Arrays; public class Jo ...