我想打印从 /tmp/values.txt 文件中读取的每一行的加密版本。任何人都可以帮我逻辑吗?以下是我的代码:
with open ('/tmp/values.txt') as f:
sentence = f.readlines()
for sent in sentence:
crypt,key = [ x for x in sent.rstrip().split('|')]
key = key.strip().split(' ')
key = list(map(int , key))
new_keys = [ (x-1) for x in key]
op = [crypt[new_keys[keys]] for keys in range(0,len(new_keys))]
print (''.join(op))
/tmp/values.txt:
Ta1HN9UPlxLfsYzEIAR4GcJQvh5biMW7oun0S8Keq6tDZmkVjX3FgB2yCwrOd p| 18 9 40 10 2 35 61 40 59 62 8 34 13 26 47 29 35 62 3 32 6 6
7NZq8pegmhAnBKH4VrGQdbPFw35jaUCzOS10oyLiv6IMXJW2 ksDRTfucElYxt9| 47 40 59 59 40 29 9 49 34 10 29 50 7 51 6 7 29 18 7 49 35 27 42 16
6qcAdESB7tlep9suiyOv8zXFg24TkaZxKLbhRUMjC3GHrI5o 10QPmVJfYWDNnw| 34 12 63 17 15 49 41 30 45 45 48 11 11 49 50 21 42 26
答案 0 :(得分:1)
我不确定文件中的行是以两个空格开头还是只是错误所以我使用strip()
代替rstrip()
而现在(正确的缩进)它适用于我。
with open ('/tmp/values.txt') as f:
sentence = f.readlines()
for sent in sentence:
crypt,key = [ x for x in sent.rstrip().split('|')]
key = key.strip().split(' ')
key = list(map(int , key))
new_keys = [ (x-1) for x in key]
op = [crypt[new_keys[keys]] for keys in range(0,len(new_keys))]
print (''.join(op))
对于测试我使用字符串而不是文件制作了这个版本:
data = ''' Ta1HN9UPlxLfsYzEIAR4GcJQvh5biMW7oun0S8Keq6tDZmkVjX3FgB2yCwrOd p| 18 9 40 10 2 35 61 40 59 62 8 34 13 26 47 29 35 62 3 32 6 6
7NZq8pegmhAnBKH4VrGQdbPFw35jaUCzOS10oyLiv6IMXJW2 ksDRTfucElYxt9| 47 40 59 59 40 29 9 49 34 10 29 50 7 51 6 7 29 18 7 49 35 27 42 16
6qcAdESB7tlep9suiyOv8zXFg24TkaZxKLbhRUMjC3GHrI5o 10QPmVJfYWDNnw| 34 12 63 17 15 49 41 30 45 45 48 11 11 49 50 21 42 26'''
for sent in data.split('\n'):
crypt,key = [ x for x in sent.strip().split('|')]
key = key.strip().split(' ')
key = list(map(int , key))
new_keys = [ (x-1) for x in key]
op = [crypt[new_keys[keys]] for keys in range(0,len(new_keys))]
print (''.join(op))
经过一些小修改后我得到了:
data = ''' Ta1HN9UPlxLfsYzEIAR4GcJQvh5biMW7oun0S8Keq6tDZmkVjX3FgB2yCwrOd p| 18 9 40 10 2 35 61 40 59 62 8 34 13 26 47 29 35 62 3 32 6 6
7NZq8pegmhAnBKH4VrGQdbPFw35jaUCzOS10oyLiv6IMXJW2 ksDRTfucElYxt9| 47 40 59 59 40 29 9 49 34 10 29 50 7 51 6 7 29 18 7 49 35 27 42 16
6qcAdESB7tlep9suiyOv8zXFg24TkaZxKLbhRUMjC3GHrI5o 10QPmVJfYWDNnw| 34 12 63 17 15 49 41 30 45 45 48 11 11 49 50 21 42 26'''
for sent in data.split('\n'):
crypt, keys = sent.strip().split('|')
keys = keys.strip().split(' ')
keys = [int(x)-1 for x in keys]
op = [crypt[x] for x in keys]
print(''.join(op))
结果:
Alexander Pushkin 1799
William Shakespeare 1564
Lewis Carroll 1832