本文介紹了在 Python 3 中刪除字符串文字前面的“b"字符的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
我是 python 編程的新手,我有點困惑.我嘗試從字符串中獲取字節(jié)以進行散列和加密,但我得到了
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'
推薦答案
解碼是多余的
你一開始只有這個錯誤",因為對正在發(fā)生的事情有誤解.
You only had this "error" in the first place, because of a misunderstanding of what's happening.
您獲得了 b
,因為您編碼為 utf-8
,現(xiàn)在它是一個字節(jié)對象.
You get the b
because you encoded to utf-8
and now it's a bytes object.
>> type("text".encode("utf-8"))
>> <class 'bytes'>
修復(fù):
- 你可以先打印字符串
- 編碼后冗余解碼
這篇關(guān)于在 Python 3 中刪除字符串文字前面的“b"字符的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!