Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/365.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 对字典文件中的消息字符串进行编码_Python_String_Dictionary_Decode_Encode - Fatal编程技术网

Python 对字典文件中的消息字符串进行编码

Python 对字典文件中的消息字符串进行编码,python,string,dictionary,decode,encode,Python,String,Dictionary,Decode,Encode,完全不工作,我一直在尝试用python对字典(两个单独的字典和代码)进行编码和解码,我对编码非常熟悉,需要帮助 我有一个短字符串,我正在尝试编码,另一个我正在尝试解码,如果这有任何意义的话。我已经阅读了这个网站上的帖子和视频,但仍然不知所措。我一直在python网站、youtube和其他网站上,找不到任何其他我已经掌握的基础知识 以下内容很好,因为它是代码的基础: 字典已创建并另存为.txt文件 #encode.txt is the dictionary being used(already c

完全不工作,我一直在尝试用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])
问题在于从字典中打印实际字符串的值。任何帮助都将不胜感激。提前谢谢

以防万一,这是字典:


我认为你需要弄清楚什么是字典(可能也是你自己)。在Python中,dictionary是一种数据类型。您的最后一行似乎将
encode
视为Python指令。但实际上
encode
是一个文件对象,这与Python完全不同。当您调用
encode.txt
字典时,它对您和我都有意义,但对Python则没有意义。您需要创建一个dictionary对象并填充它,然后才能像在最后一行中尝试做的那样从中获取某些内容。如果您显示一个精简的
encode.txt
文件(例如,仅2行),然后显示您想要编写的函数的原型,这会有所帮助,文本文件的内容是问题末尾的3行Python代码?如果是这样,当您
打开
该文件,然后执行
以输入
结果时,它将在3行上迭代。(然后,在同一文件对象上附加的两个
for
循环根本不会起任何作用。)
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',' ':'','!':'!'}