Python2以字母数字格式递增字符串

时间:2017-10-11 21:22:44

标签: string python-2.7 alphanumeric

我正在寻找一个类似于以下内容的片段:

from 'abcd8' string
i want to increment it like 'abcd9' then 'abcda' ... >'abcdz' and next     'abce0', 'abce1' ... 'abcez', 'abcf0' ....

无法找到办法:(

我已经尝试过使用'aaaa'.encode('hex')然后增加它,但不能正常工作:(

感谢您的帮助,

干杯!

1 个答案:

答案 0 :(得分:0)

这是一种方法:

# written by 'imerso' to a StackOverflow answer
def increment(os):
    s = list(os)
    p = len(os)-1

    while (p >= 0):
        s[p] = chr(ord(s[p]) + 1)

        if s[p] > 'z':
            s[p] = '0'
            p -= 1
            continue

        if s[p] > '9' and s[p] < 'a':
            s[p] = 'a'

        return ''.join(s)


s = raw_input("String: " )

for x in range(1, 32):
    s = increment(s)
    print s