![](/img/trans.png)
[英]How do I take a number from a text file and replace it with that number +1?
[英]How do I take each letter or character from the text file, compare it to the KEY in the dictionary, and replace it with the VALUE?
我正在做一个非常基本的加密/解密程序。 我已经以字典的形式给了密码。 我需要做的就是从文本文件中读取文本,然后使用字典进行加密或解密。 我以为我已经弄明白了,但是我遇到了麻烦。
我的问题:如何从文本文件中提取每个字母或字符,将其与字典中的KEY进行比较,然后将其替换为VALUE?
现在,我可以使用Dict.get定位KEY,但是我不知道如何进行加密。
码:
def convert(inputFile, outputFile):
# Encryption and decryption are inverse of one another
CODE = {'A':')','a':'0','B':'(','b':'9','C':'*','c':'8',\
'D':'&','d':'7','E':'^','e':'6','F':'%','f':'5',\
'G':'$','g':'4','H':'#','h':'3','I':'@','i':'2',\
'J':'!','j':'1','K':'Z','k':'z','L':'Y','l':'y',\
'M':'X','m':'x','N':'W','n':'w','O':'V','o':'v',\
'P':'U','p':'u','Q':'T','q':'t','R':'S','r':'s',\
'S':'R','s':'r','T':'Q','t':'q','U':'P','u':'p',\
'V':'O','v':'o','W':'N','w':'n','X':'M','x':'m',\
'Y':'L','y':'l','Z':'K','z':'k','!':'J','1':'j',\
'@':'I','2':'i','#':'H','3':'h','$':'G','4':'g',\
'%':'F','5':'f','^':'E','6':'e','&':'D','7':'d',\
'*':'C','8':'c','(':'B','9':'b',')':'A','0':'a',\
':':',',',':':','?':'.','.':'?','<':'>','>':'<',\
"'":'"','"':"'",'+':'-','-':'+','=':';',';':'=',\
'{':'[','[':'{','}':']',']':'}'}
f = open(inputFile, 'r')
result = ''
for key in f:
result += CODE.get(key, key)
f.close()
print(result)
for key in f
循环遍历文件中的行,而不是字符。 您需要遍历该行中的字符。
for key in f:
result += ''.join([CODE.get(char, char) for char in key])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.