TypeError:' int'尝试解码base62时,object不可迭代

时间:2014-04-12 19:39:03

标签: python math flask base62

我正在尝试编写decode base62函数,但是python给了我错误:

TypeError: 'int' object is not iterable

此代码在烧瓶外完美地完成。但它在烧瓶中不起作用。

代码如下:已编辑:

BASE_ALPH = tuple("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")
BASE_DICT = dict((c, v) for v, c in enumerate(BASE_ALPH))
BASE_LEN = len(BASE_ALPH)

def base62_decode(string):
    tnum = 0
    for char in str(string):
        tnum = tnum * BASE_LEN + BASE_DICT[char]
    return tnum


def base62_encode(num):
    if not num:
        return BASE_ALPH[0]
    if num<0:
        return False
    num = int(num)
    encoding = ""
    while num:
        num, rem = divmod(num, BASE_LEN)
        encoding = BASE_ALPH[rem] + encoding
    return encoding

这段代码在烧瓶外工作得非常好,但是当我从Flask应用程序中调用它时,会出现错误。

1 个答案:

答案 0 :(得分:0)

尝试强制转换为字符串,看看它是否能让你运行没有错误,然后检查你的解码输出 - 我想在某些时候你有一个字符串代表一个由python内部转换的数字,而且&# 39;错误来自的地方:

def base62_decode(s):
    tnum = 0
    for char in str(s):
        tnum = tnum * BASE_LEN + BASE_DICT[char]
    return tnum

请注意,此代码假定为python 2;迭代字符串时,python 3的行为有所不同。

相关问题