gpt4 book ai didi

python - 像使用不同索引表的字符串一样解码 base64

转载 作者:太空狗 更新时间:2023-10-29 21:32:41 24 4
gpt4 key购买 nike

我的问题是,我有一些用不同索引表编码的东西(类似 base64):

0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/

代替

ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/

所以当我使用 base64.b64decode() 时,它给了我一个错误的结果。有没有办法在转换期间设置此表(可能作为参数)?

或者我应该“转换”错误的 base64 字符串,我的意思是将 0 替换为 A,将 1 替换为 B,等等...而不是使用 base64decode?如果是这样,最好和最快速的解决方法是什么?

update1:我用的是这个,可以用,但看起来有点慢,而且不专业。 :)

def correctbase64(str):
dicta = [ ['0','A'], ['1','B'], ['2','C'], ['3','D'], ['4','E'], ['5','F'], ['6','G'], ['7','H'], ['8','I'], ['9','J'], ['A','K'], ['B','L'], ['C','M'], ['D','N'], ['E','O'], ['F','P'], ['G','Q'], ['H','R'], ['I','S'], ['J','T'], ['K','U'], ['L','V'], ['M','W'], ['N','X'], ['O','Y'], ['P','Z'], ['Q','a'], ['R','b'], ['S','c'], ['T','d'], ['U','e'], ['V','f'], ['W','g'], ['X','h'], ['Y','i'], ['Z','j'], ['a','k'], ['b','l'], ['c','m'], ['d','n'], ['e','o'], ['f','p'], ['g','q'], ['h','r'], ['i','s'], ['j','t'], ['k','u'], ['l','v'], ['m','w'], ['n','x'], ['o','y'], ['p','z'], ['q','0'], ['r','1'], ['s','2'], ['t','3'], ['u','4'], ['v','5'], ['w','6'], ['x','7'], ['y','8'], ['z','9'] ]
l = list(str)
for i in range(len(l)):
for c in dicta:
if l[i] == c[0]:
l[i] = c[1]
break
return "".join(l)

最佳答案

像这样的东西应该可以工作(警告:未经测试的代码;可能充满错误):

import string

my_base64chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/"
std_base64chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"

s = s.translate(string.maketrans(my_base64chars, std_base64chars))
data = base64.b64decode(s)

不可能使标准 base64 函数(或它们调用的 binascii 中的低级函数)使用自定义表。

关于python - 像使用不同索引表的字符串一样解码 base64,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5537750/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com