蟒蛇2.7
您如何获取任何数字并能够将其压缩为小写字母和数字?以及如何获取结果字符串并将其放回数字?
对每个 3 个数字的数字块做类似 chr() 的操作是行不通的,因为如果数字块大于 255,它会抛出一个错误,我只想要小写数字和字母。
编辑:
它的主要目的是压缩一个数字。用户将看到并输入它,因此它需要易于输入(不能有 ascii 支持但不在标准键盘上的奇怪字符)
使用base 36,以下是您可以在 Wikipedia 上找到的 Python 实现的改编版本:
def base36encode(number, alphabet='0123456789abcdefghijklmnopqrstuvwxyz'):
"""Converts an integer to a base36 string."""
if not isinstance(number, (int, long)):
raise TypeError('number must be an integer')
base36 = '' if number != 0 else '0'
sign = ''
if number < 0:
sign = '-'
number = -number
while number != 0:
number, i = divmod(number, len(alphabet))
base36 = alphabet[i] + base36
return sign + base36
def base36decode(number):
return int(number, 36)
>>> print base36encode(1412823931503067241)
aqf8aa0006eh
>>> print base36decode('aqf8aa0006eh')
1412823931503067241
str.lower()
makes a string str lowercase. Alternatively, you could use regex to only find letters/words and numbers, but the question is not very clear, so I'm not sure what you mean.