在 Python 3 中删除字符串文字前面的“b"字符
- 作者: 最污大湿兄
- 来源: 51数据库
- 2022-10-25
问题描述
我是 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'>
修复:
- 你可以先打印字符串
- 编码后冗余解码
推荐阅读
热点文章
Discord.py(重写)on_member_update 无法正常工作
0
Discord.py 在 vc 中获取用户分钟数
0
discord.py 重写 |为我的命令出错
0
Discord.py rewrite 如何 DM 命令?
0
播放音频时,最后一部分被切断.如何解决这个问题?(discord.py)
0
在消息删除消息 Discord.py
0
如何使 discord.py 机器人私人/直接消息不是作者的人?
0
(Discord.py) 如何获取整个嵌入内容?
0
Discord bot 尽管获得了许可,但不能提及所有人
0
Discord.py discord.NotFound 异常
0
