-4

我正在对以下给定的以 base64 格式编码的反向多行字符串进行解码。

LnNlbHBtYXhlIGVzdSBvdCBlZXJmIGxlZUYgLnNldGlzYmV3IGNpZmZhcnQgaGdpaCBubyBub2l0YXNpbGFtcm9uZWQgZm8gdHBlY25vYyBlaHQgZWJpcmNzZUQgLjQ= ZWxpZiBlbm8gbmkgZWIgXYdH

s = 'ZWxpZiBlbm8gbmkgZWIgdHN1TSApaXYgICAg'
s = s.decode('base64', 'strict')

print s[::-1]

通过上述方法,我得到了正确的字符串,但我必须手动更改字符串。

请帮忙。

4

1 回答 1

2

从OP的评论:

list = ['LnNlbHBtYXhlIGVzdSBvdCBlZXJmIGxlZUYgLnNldGlzYmV3IGNpZmZhcnQgaGdpaCBubyBub2l0YXNpbGFtcm9uZWQgZm8gdHBlY25vYyBlaHQgZWJpcmNzZUQgLjQ=', 'ZWxpZiBlbm8gbmkgZWIgdHN1TSApaXYgICAg', 'c25vaXRhY2lmaWNlcHMgOC1QRVAgdGVlbSB0c3VNICl2ICAgIA==', 'Ni4yIG5vaHR5UCBodGl3IGtyb3cgdHN1TSApdmkgICAg', 'c2VsdWRvbSByZWh0byB5YiBlbGJhdHJvcG1pIGViIGRsdW9ocyBzc2FsQyApaWlpICAgIA==']
s = ''.join(list)
s = s.decode('base64', 'strict')
print (s[::-1])

join操作将所有字符串连接在一起,但只翻译第一个字符串。这是因为,在解码 base 64 字符串时,字符串中前一个或两个=字符之后的所有内容都将被忽略。不是加入,而是迭代:

list = ['LnNlbHBtYXhlIGVzdSBvdCBlZXJmIGxlZUYgLnNldGlzYmV3IGNpZmZhcnQgaGdpaCBubyBub2l0YXNpbGFtcm9uZWQgZm8gdHBlY25vYyBlaHQgZWJpcmNzZUQgLjQ=', 'ZWxpZiBlbm8gbmkgZWIgdHN1TSApaXYgICAg', 'c25vaXRhY2lmaWNlcHMgOC1QRVAgdGVlbSB0c3VNICl2ICAgIA==', 'Ni4yIG5vaHR5UCBodGl3IGtyb3cgdHN1TSApdmkgICAg', 'c2VsdWRvbSByZWh0byB5YiBlbGJhdHJvcG1pIGViIGRsdW9ocyBzc2FsQyApaWlpICAgIA==']
for s in list:
    s = s.decode('base64', 'strict')
    print (s[::-1])

或者使用列表理解。

list = ['LnNlbHBtYXhlIGVzdSBvdCBlZXJmIGxlZUYgLnNldGlzYmV3IGNpZmZhcnQgaGdpaCBubyBub2l0YXNpbGFtcm9uZWQgZm8gdHBlY25vYyBlaHQgZWJpcmNzZUQgLjQ=', 'ZWxpZiBlbm8gbmkgZWIgdHN1TSApaXYgICAg', 'c25vaXRhY2lmaWNlcHMgOC1QRVAgdGVlbSB0c3VNICl2ICAgIA==', 'Ni4yIG5vaHR5UCBodGl3IGtyb3cgdHN1TSApdmkgICAg', 'c2VsdWRvbSByZWh0byB5YiBlbGJhdHJvcG1pIGViIGRsdW9ocyBzc2FsQyApaWlpICAgIA==']
print "\n".join(s.decode('base64')[::-1] for s in list)

输出:

4. Describe the concept of denormalisation on high traffic websites. Feel free to use examples.
    vi) Must be in one file
    v) Must meet PEP-8 specifications
    iv) Must work with Python 2.6
    iii) Class should be importable by other modules
于 2012-12-13T17:33:23.000 回答