Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python I';我一直在做替换_Python_Encryption - Fatal编程技术网

Python I';我一直在做替换

Python I';我一直在做替换,python,encryption,Python,Encryption,我正在制作的程序包括3个文件,加密文本文件(words.txt),一个包含3条线索的文件(clues.txt)和一个已解决的文本文件(我们不需要担心)。 到目前为止,我已经做到了这一点: print('This is the encrypted code:') CodedFile = open('words.txt' , 'r+') print(CodedFile.read()) CodedFile.close() time.sleep(3) print() print('These are a

我正在制作的程序包括3个文件,加密文本文件(words.txt),一个包含3条线索的文件(clues.txt)和一个已解决的文本文件(我们不需要担心)。 到目前为止,我已经做到了这一点:

print('This is the encrypted code:')
CodedFile = open('words.txt' , 'r+')
print(CodedFile.read())
CodedFile.close()
time.sleep(3)
print()
print('These are a few hints to get you started:')
HintFile = open('clues.txt' , 'r+')
print(HintFile.read())
HintFile.close()
time.sleep(2)

现在我必须从提示文件中获取3个提示并将它们替换为加密代码,这三个提示是A=#M=*N=%。其中一个单词是ALMANAC,它将是#3*#%#+加密的。如何从#3*#%#+到A3MANA+,仅使用提示部分,将非常感谢您的帮助

你在找这样的东西吗:

codedFile = open("words.txt")
encryptedText = codedFile.read()
codedFile.close()

cluesFile = open("clues.txt")
cluesLines = cluesFile.readlines()
cluesFile.close()

clues = {}
for line in cluesLines:
    clues[line[2]] = line[0]

print(''.join(clues.get(c, c) for c in encryptedText))
cluesFile.readlines()
读取所有文件并返回行列表:
['a=\n',M=*\n',n=%\n']

线索
是一本代词词典。对于循环,在
之后,它将等于
{'%':'N','#':'A','*':'M'}


如果存在替换,最后一行将
encryptedText
中的每个字符
c
替换为
线索中的字符。方法
get(key,default)
字典返回指定给key
key
default
的值,如果没有这样的键。

您知道使用什么密码,或者至少知道它是如何工作的吗?类似于带映射的词典?你需要尝试一些东西,然后问一个特定的问题。您发布的代码几乎相当于
cat words.txt clues.txt
。程序中的整个要点是,用户可以找出每个符号所替代的字母。所以没有使用密码。看起来没问题,但出于某种原因,“线索[line[2]]=line[0]”导致语法错误,因为字符串索引超出范围,这是为什么?@user3033630,
索引超出范围
表示您试图访问元素,而该元素不在
中。原因可能是
clues.txt
的内容错误。例如,它的末尾可能包含空行。如果是这样,您可以将空性检查添加到循环中。如果没有帮助,请将调试输出添加到for cycle,以了解错误原因。