Python2 以字母数字格式递增字符串
Python2 increment string in an alphanumeric format
我正在寻找一个片段,它可以做类似的事情:
from 'abcd8' string
i want to increment it like 'abcd9' then 'abcda' ... >'abcdz' and next 'abce0', 'abce1' ... 'abcez', 'abcf0' ....
找不到方法:(
我已经尝试过使用 'aaaa'.encode('hex') 然后递增它,但没有用:(
感谢您的帮助,
干杯!
这里有一个方法:
# written by 'imerso' to a Whosebug 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