python base64解码TypeE

2023-01-31 02:01:21 python base64 解码
直接贴代码
>>> b='5paw5Lqn5ZOB55qE5biC5Zy6566h55CGMWk'
>>> base64.b64decode(b)
Traceback (most recent call last):
  File "", line 1, in
  File "/usr/lib64/python2.6/base64.py", line 76, in b64decode
    raise TypeError(msg)
TypeError: Incorrect padding

百度了一下找到国外的一篇文章
Http://stackoverflow.com/questions/2941995/Python-ignore-incorrect-padding-error-when-base64-decoding
解决办法如下:
  lens = len(strg) lenx = lens - (lens % 4 if lens % 4 else 4) try:     result = base64.decodestring(strg[:lenx]) except etc 

相关文章