在Python 3中删除字符串文字前面的'b'字符do [英] Remove 'b' character do in front of a string literal in Python 3
本文介绍了在Python 3中删除字符串文字前面的'b'字符do的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是python编程的新手,我有点困惑.我尝试从字符串中获取字节以进行哈希和加密,但是我得到了
I am new in python programming and i am a bit confused. I try to get the bytes from a string to hash and encrypt but i got
b'...'
像下面的示例一样,字符串前面的
b字符.有什么办法可以避免这种情况吗?有人可以提供解决方案吗?很抱歉这个愚蠢的问题
b character in front of string just like the below example. Is any way avoid this?.Can anyone give a solution? Sorry for this silly question
import hashlib
text = "my secret data"
pw_bytes = text.encode('utf-8')
print('print',pw_bytes)
m = hashlib.md5()
m.update(pw_bytes)
输出:
print b'my secret data'
推荐答案
解码是多余的
由于对发生的事情有误解,所以您只遇到了这个错误".
You only had this "error" in the first place, because of a misunderstanding of what's happening.
之所以会得到b
,是因为您已将其编码为utf-8
,现在它是一个字节对象.
You get the b
because you encoded to utf-8
and now it's a bytes object.
>> type("text".encode("utf-8"))
>> <class 'bytes'>
修复:
- 您可以先打印字符串
- 编码后冗余解码
这篇关于在Python 3中删除字符串文字前面的'b'字符do的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文