python字典键-值的替换
sign_table = { #解码 : . / 这三个符号
'_z2C$q': ':',
'_z&e3B': '.',
'AzdH3F': '/'
}
char_table = { #解码其他字符
'w': 'a',
'k': 'b',
'v': 'c',
'1': 'd',
'j': 'e',
'u': 'f',
'2': 'g',
'i': 'h',
't': 'i',
'3': 'j',
'h': 'k',
's': 'l',
'4': 'm',
'g': 'n',
'5': 'o',
'r': 'p',
'q': 'q',
'6': 'r',
'f': 's',
'p': 't',
'7': 'u',
'e': 'v',
'o': 'w',
'8': '1',
'd': '2',
'n': '3',
'9': '4',
'c': '5',
'm': '6',
'0': '7',
'b': '8',
'l': '9',
'a': '0'
}
char_table = {ord(key): ord(value) for key, value in char_table.items()}
def decode(url):
# 先替换字符串
for key, value in sign_table.items():
url = url.replace(key, value)
# 再替换剩下的字符
return url.translate(char_table)
if __name__ == '__main__':
url = 'ippr_z2C$qAzdH3FAzdH3Ft42f6v_z&e3Bkwt17_z&e3Bv54AzdH3Fu5674AzdH3Fo=cbaAzdH3Fft2g' \
'=vd9wwmmca8nkckkckj10daumam1d1cdnAzdH3Fjd1vubaj0kjvc9j01vwuk11akknblkca91vdmwwc_z&e3B3r2'
print(decode(url))
实验:先检查有没有符合字典a中的字符串,有着换,无则过。(利用函数ord,字典函数的替换replance)
而后将检索符合字典aold的字符串
a = {
'ad': '//',
'ac': ':',
'ab': '+'
}
aold = {'a': '1',
'b': '2',
'c': '3',
'd': '4'}
aold = {ord(key): ord(value) for key, value in aold.items()}
def fun(url):
for key, value in a.items():
url = url.replace(key, value)
return url.translate(aold)
if __name__ == '__main__':
url = input(":")
print(fun(url))