完全不工作,我一直在尝试在 python 中从字典(两个单独的字典和代码)进行编码和解码,我对编码非常熟悉,需要帮助。
我有一个我正在尝试编码的短字符串和另一个我正在尝试解码的字符串,如果这有任何意义的话。我已经阅读了该网站上的帖子以及有关信息的视频,但仍然不知所措。我一直在 python 网站、youtube 和其他网站上,除了我已经拥有的基础知识之外找不到其他任何东西。
以下工作正常,因为它是代码的基础:
字典已创建并保存为 .txt 文件。
#encode.txt is the dictionary being used(already created and saved as a .txt file)
#saved in the same folder as the python code that I am creating(also saved on my system).
encode = open ("encode.txt", "r")
#lines below are examples of Basic code that I understand, not a part of my actual code
for key in encode:
print(key)
for val in encode:
print (val)
#started with something similar to this and tried adding string in, yet wont work this way
#this will only print the actual dictionary info(key,val)
#needing to print values of a string, not just single character from the dictionary
for key in encode:
print(key,encode[key])
问题出现的地方是从字典中打印实际字符串的值。任何帮助将不胜感激。提前致谢。
以防万一,这是字典:
encode = {'b':'a','B':'A','c':'e','C':'E','d':'i','D':'I','f':'o','F':'O','g':'u','G':'U','h':'b','H':'B','j':'c','J':'C','k':'d','K':'D','l':'f','L':'F','m':'g','M':'G','n':'h','N':'H','p':'j','P':'J','q':'k','Q':'K','r':'l','R':'L','s':'m','S':'M','t':'n','T':'N','v':'p','V':'P','w':'q','W':'Q','x':'r','X':'R','y':'s','Y':'S','Z':'t','z':'T','a':'v','A':'V','e':'w','E':'W','i':'x','I':'X','o':'y','O':'Y','u':'Z','U':'z',' ':'','!':'!'}
decode = {'a':'b','A':'B','e':'c','E':'C','i':'d','I':'D','o':'f','O':'F','u':'g','U':'G','b':'h','B':'H','c':'j','C':'J','d':'k','D':'K','f':'l','F':'L','g':'m','G':'M','h':'n','H':'N','j':'p','J':'P','k':'q','K':'Q','l':'r','L':'R','m':'s','M':'S','n':'t','N':'T','p':'v','P':'V','q':'w','Q':'W','r':'x','R':'X','s':'y','S':'Y','t':'Z','T':'z','v':'a','V':'A','w':'e','W':'E','x':'i','X':'I','y':'o','Y':'O','Z':'u','z':'U',' ':'','!':'!'}