设为首页 加入收藏

TOP

Python base64解码TypeError: Incorrect padding错误处理
2014-11-23 22:59:17 来源: 作者: 【 】 浏览:10
Tags:Python base64 解码 TypeError: Incorrect padding 错误 处理

Python 自带的base64库,在解码base64字符串的时候抛异常 TypeError: Incorrect padding。

代码如下:


>>> b='5paw5Lqn5ZOB55qE5biC5Zy6566h55CGMWk'
>>> base64.b64decode(b)
Traceback (most recent call last):
File "", line 1, in
File "D:\Python27\Lib\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:
pass


其中strg是需要解码的base64字符串


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Android应用Theme 下一篇C++11新特性中的匿名函数Lambda表..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: