Python base64編碼解碼

2021-10-09 11:58:09 字數 1069 閱讀 5279

錯誤資訊:

typeerror: a bytes

-like object

is required,

not'str'

base64.b64encode()方法的引數必須是乙個bytes-like object型別

s =

'abcd'

s =bytes

(s, encoding=

'utf-8'

)# 或者是

s =str

.encode(s,

'utf-8'

)# 或者是

s = s.encode(

'utf-8'

)# 輸出的結果都是一樣的

print

(s)# b'abcd'

b = b'abcd'

b =str

(b, encoding=

'utf-8'

)# 或者是

b =bytes

.decode(b)

# encoding 預設是 'utf-8

# 或者是

b = b.decode(

)# encoding 預設是 'utf-8

s =

'abcd'

# str 型別編碼成 bytes 型別

b_s = s.encode(

'utf-8'

)print

(b_s)

# b'abcd'

# base64 加密

m_s = base64.b64encode(b_s)

print

(m_s)

# b'ywjjza=='

# bytes 型別解碼成 str 型別

s_s =

str(m_s,

'utf-8'

)print

(s_s)

# ywjjza==

Python base64編碼和解碼

首先在python2中base的編碼和解碼 1 s 我是乙個字串 2 編碼3base64.b64encode s 4 解碼5 base64.b64decode s 在python3中和python2中是稍微有一點區別的。1 s 我是乙個字串 2 編碼3 base64.b64encode s.enco...

python base64加密和解密

base64可用加密和解密,為python內建模組,可以實現base64 base32 base16 base85 urlsafe base64的編碼解碼,python 3.x通常輸入輸出都是二進位制形式,2.x可以是字串形式。base64模組的base64編碼 解碼呼叫了binascii模組,bi...

Python Base64轉碼解碼

python base64 提供了好幾種方法例如 encode,decode,encodestring,decodestring,b64encode,b64decode,standard b64encode standard b64decode urlsafe b64decode,urlsafe b...