我制作的字典用短划线( - )和两个数字代替字母。
在我的字典中,“t”=“ - 21”。当我从字典中调用“t”时,我得到“-21”。但是当我打电话给replace_all(text, dic)
时,输出是“-2--5-63-5-63”
知道出了什么问题吗?
Python 2.7.10 (default, May 23 2015, 09:44:00) [MSC v.1500 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ldic3 = {"a":"-02","b":"-03","c":"-04","d":"-05","e":"-06","f":"-07","g":"-08","h":"-09","i":"-10","j":"-11","k":"-12","l":"-13","m":"-14","n":"-15","o":"-16","p":"-17","q":"-18","r":"-19","s":"-20","t":"-21","u":"-22","v":"-23","w":"-24","x":"-25","y":"-26","z":"-27","A":"-28","B":"-29","C":"-30","D":"-31","E":"-32","F":"-33","G":"-34","H":"-35","I":"-36","J":"-37","K":"-38","L":"-39","M":"-40","N":"-41","O":"-42","P":"-43","Q":"-44","R":"-45","S":"-46","T":"-47","U":"-48","V":"-49","W":"-50","X":"-51","Y":"-52","Z":"-53","0":"-54","1":"-55","2":"-56","3":"-57","4":"-58","5":"-59","6":"-60","7":"-61","8":"-62","9":"-63"}
>>> def replace_all(text, dic):
for i, j in dic.iteritems():
text = text.replace(i, j)
return text
#Example A
>>> var = "t"
>>> var2 = replace_all(var, ldic3)
>>> var2
'-2--5-63-5-63'
>>> ldic3["t"]
'-21'
#Example B
>>> var = "a"
>>> var2 = replace_all(var, ldic3)
>>> var2
'-02'
>>>
快速修改:
这意味着通过一遍又一遍地改变它来使文本变得不可读。我不能把我的字符串变成自己的字典,而不会破坏我的程序。这是为了一次更改整个段落中的字符。
此外,“replace_all(text,dic)”的代码是我在网上找到的,我不确定它是如何工作的。
这是我的计划:
from Tkinter import *
import time
def replace_all(text, dic):
for i, j in dic.iteritems():
text = text.replace(i, j)
return text
passchar = {"a":"0","b":"1","c":"2","d":"3","e":"0","f":"1","g":"2","h":"3","i":"0","j":"1","k":"2","l":"3","m":"0","n":"1","o":"2","p":"3","q":"0","r":"1","s":"2","t":"3","u":"0","v":"1","w":"2","x":"3","y":"0","z":"1"," ":""}
ldic0 = {"a":"-82","b":"-93","c":"-11","d":"-91","e":"-88","f":"-27","g":"-01","h":"-17","i":"-72","j":"-90","k":"-99","l":"-42","m":"-21","n":"-12","o":"-81","p":"-28","q":"-73","r":"-96","s":"-92","t":"-87","u":"-86","v":"-62","w":"-00","x":"-31","y":"-69","z":"-77","A":"-02","B":"-09","C":"-43","D":"-10","E":"-46","F":"-78","G":"-13","H":"-24","I":"-35","J":"-46","K":"-05","L":"-83","M":"-96","N":"-70","O":"-98","P":"-48","Q":"-29","R":"-32","S":"-52","T":"-94","U":"-53","V":"-66","W":"-44","X":"-51","Y":"-71","Z":"-80","0":"-97","1":"-39","2":"-26","3":"-54","4":"-22","5":"-07","6":"-40","7":"-30","8":"-50","9":"-60"}
ldic1 = {"a":"-01","b":"-02","c":"-03","d":"-04","e":"-05","f":"-06","g":"-07","h":"-08","i":"-09","j":"-10","k":"-11","l":"-12","m":"-13","n":"-14","o":"-15","p":"-16","q":"-17","r":"-18","s":"-19","t":"-20","u":"-21","v":"-22","w":"-23","x":"-24","y":"-25","z":"-26","A":"-27","B":"-28","C":"-29","D":"-30","E":"-31","F":"-32","G":"-33","H":"-34","I":"-35","J":"-36","K":"-37","L":"-38","M":"-39","N":"-40","O":"-41","P":"-42","Q":"-43","R":"-44","S":"-45","T":"-46","U":"-47","V":"-48","W":"-49","X":"-50","y":"-51","Z":"-52","0":"-53","1":"-54","2":"-55","3":"-56","4":"-57","5":"-58","6":"-59","7":"-60","8":"-61","9":"-62"}
ldic2 = {"a":"-02","b":"-03","c":"-04","d":"-05","e":"-06","f":"-07","g":"-08","h":"-09","i":"-10","j":"-11","k":"-12","l":"-13","m":"-14","n":"-15","o":"-16","p":"-17","q":"-18","r":"-19","s":"-20","t":"-21","u":"-22","v":"-23","w":"-24","x":"-25","y":"-26","z":"-27","A":"-28","B":"-29","C":"-30","D":"-31","E":"-32","F":"-33","G":"-34","H":"-35","I":"-36","J":"-37","K":"-38","L":"-39","M":"-40","N":"-41","O":"-42","P":"-43","Q":"-44","R":"-45","S":"-46","T":"-47","U":"-48","V":"-49","W":"-50","X":"-51","Y":"-52","Z":"-53","0":"-54","1":"-55","2":"-56","3":"-57","4":"-58","5":"-59","6":"-60","7":"-61","8":"-62","9":"-63"}
ldic3 = {"a":"-02","b":"-03","c":"-04","d":"-05","e":"-06","f":"-07","g":"-08","h":"-09","i":"-10","j":"-11","k":"-12","l":"-13","m":"-14","n":"-15","o":"-16","p":"-17","q":"-18","r":"-19","s":"-20","t":"-21","u":"-22","v":"-23","w":"-24","x":"-25","y":"-26","z":"-27","A":"-28","B":"-29","C":"-30","D":"-31","E":"-32","F":"-33","G":"-34","H":"-35","I":"-36","J":"-37","K":"-38","L":"-39","M":"-40","N":"-41","O":"-42","P":"-43","Q":"-44","R":"-45","S":"-46","T":"-47","U":"-48","V":"-49","W":"-50","X":"-51","Y":"-52","Z":"-53","0":"-54","1":"-55","2":"-56","3":"-57","4":"-58","5":"-59","6":"-60","7":"-61","8":"-62","9":"-63"}
#ldic3 = {"a":"-62","b":"-61","c":"-60","d":"-59","e":"-58","f":"-57","g":"-56","h":"-55","i":"-54","j":"-53","k":"-52","l":"-51","m":"-50","n":"-49","o":"-48","p":"-47","q":"-46","r":"-45","s":"-44","t":"-43","u":"-42","v":"-41","w":"-40","x":"-39","y":"-38","z":"-37","A":"-36","B":"-35","C":"-34","D":"-33","E":"-32","F":"-31","G":"-30","H":"-29","I":"-28","J":"-27","K":"-26","L":"-25","M":"-24","N":"-23","O":"-22","P":"-21","Q":"-20","R":"-19","S":"-18","T":"-17","U":"-16","V":"-15","W":"-14","X":"-13","Y":"-12","Z":"-10","0":"-09","1":"-08","2":"-07","3":"-06","4","-05","5":"-04","6":"-03","7":"-02","8":"-01","9":"-00"}
ndic0 = {"0":"3","1":"4","2":"1","3":"7","4":"9","5":"8","6":"2","7":"5","8":"6","9":"0"}
ndic1 = {"0":"1","1":"4","2":"3","3":"5","4":"0","5":"8","6":"2","7":"6","8":"9","9":"7"}
ndic2 = {"0":"2","1":"9","2":"8","3":"4","4":"7","5":"3","6":"0","7":"1","8":"6","9":"5"}
ndic3 = {"0":"4","1":"2","2":"3","3":"9","4":"0","5":"1","6":"7","7":"8","8":"6","9":"5"}
class Application(Frame):
def __init__(self, master):
#Initialize the frame
Frame.__init__(self,master)
self.grid()
self.create_widgets()
def create_widgets(self):
self.instruction = Label(self, text = "ENCRYPTION KEY:")
self.instruction.grid(row = 0, column = 1, columnspan = 2, sticky = N) #sticky = direction W = West
self.key = Entry(self) #Promps for user input
self.key.grid(row = 1, column = 2, sticky = N)
#SPACER1
self.spacer1 = Label(self, text = " ")
self.spacer1.grid(row = 2, column = 0, columnspan = 2, sticky = N)
#-------------
#ENCRYPT BUTTON
self.encrypt_button = Button(self, width = 18, height = 2, text ="ENCRYPT", command = self.textboxa1)
self.encrypt_button.grid(row = 3, column = 0, sticky = N)
#CLEAR BUTTON
self.clear_button = Button(self, text ="CLEAR", width = 18, command = self.clearboxes)
self.clear_button.grid(row = 3, column = 2, sticky = N)
#DECRYPT BUTTON
self.decrypt_button = Button(self, width = 18, height = 2, text ="DECRYPT",) #command = self.textboxa2
self.decrypt_button.grid(row = 3, column = 3, sticky = N)
#Top TextBox
self.textbox1 = Text(self, width = 50, height = 15, wrap = WORD) # wrap = WORD CHAR or NONE
self.textbox1.grid(row = 4, column = 0, columnspan = 4, sticky = N)
#-------------
#SPACER2
self.spacer2 = Label(self, text = " ")
self.spacer2.grid(row = 5, column = 0, columnspan = 2, sticky = N)
#Bottom TextBox
self.textbox2 = Text(self, width = 50, height = 15, wrap = WORD)
self.textbox2.grid(row = 6, column = 0, columnspan = 4, sticky = N)
def textboxa1(self):
keyvar = self.key.get()
textvar = self.textbox1.get(0.0, END)
keyvarl = len(keyvar)
if keyvarl > 25 or keyvarl < 3:
self.textbox2.delete(0.0, END)
self.textbox2.insert(0.0, '"' + keyvar + '"' + " -ERROR PASSWORD MUST BE 3 - 25 CHARACTERS LONG")
else:
step = 0
num = 0
passcode = replace_all(keyvar, passchar)
#self.textbox2.delete(0.0, END)
#self.textbox2.insert(0.0, "start")
for i in range(keyvarl):
char = list(passcode)
dig = passcode[num]
self.textbox2.insert(0.0, "")
if dig == "0":
if step == 0:
step = 1
textvar2 = replace_all(textvar, ldic0)
#time.sleep(0.5)
#self.textbox2.insert(0.0, "0")
else:
textvar2 = replace_all(textvar2, ndic0)
#time.sleep(0.5)
#self.textbox2.insert(0.0, "0")
elif dig == "1":
if step == 0:
step = 1
textvar2 = replace_all(textvar, ldic1)
#time.sleep(0.5)
#self.textbox2.insert(0.0, "1")
else:
textvar2 = replace_all(textvar2, ndic1)
#time.sleep(0.5)
#self.textbox2.insert(0.0, "1")
elif dig == "2":
if step == 0:
step = 1
textvar2 = replace_all(textvar, ldic2)
#time.sleep(0.5)
#self.textbox2.insert(0.0, "2")
else:
textvar2 = replace_all(textvar2, ndic2)
#time.sleep(0.5)
#self.textbox2.insert(0.0, "2")
else:
if step == 0:
step = 1
textvar2 = replace_all(textvar, ldic3)
#time.sleep(0.5)
#self.textbox2.insert(0.0, "3")
else:
textvar2 = replace_all(textvar2, ndic3)
#time.sleep(0.5)
#self.textbox2.insert(0.0, "3")
keyvar2 = keyvarl - 1
if i == keyvar2:
#self.textbox2.delete(0.0, END)
self.textbox2.insert(0.0, textvar2 + textvar)
else:
#self.textbox2.delete(0.0, END)
self.textbox2.insert(0.0, textvar2 + textvar)
self.textbox2.insert(0.0, keyvar2)
self.textbox2.insert(0.0, " of ")
self.textbox2.insert(0.0, num)
self.textbox2.insert(0.0, "Step: ")
# self.textbox2.insert(0.0, textvar2 + textvar)
num = num + 1
# def textboxa2(self):
# input1 = self.key.get()
# input2 = self.textbox1.get(0.0, END)
# self.textbox2.delete(0.0, END)
# self.textbox2.insert(0.0, input1 + input2)
def clearboxes(self):
self.textbox1.delete(0.0, END)
self.textbox2.delete(0.0, END)
root = Tk()
root.title("FirePie Demo - Message Encryptor!")
root.geometry("403x630")
app = Application(root)
root.mainloop()
答案 0 :(得分:2)
“t”替换为“-21”
然后将“1”替换为“-55”
然后两个“5”被替换为“-59”,结果为“-2--59-59”
然后两个“9”被替换为“-63”,结果为“-2--5-63-5-63”,返回。
如果您不想替换之前替换之一所产生的字母,请考虑立即更换一个字母:
def replace_all(text, dic):
return ''.join(dic.get(x, x) for x in text)
x
中的第二个get
是dict
没有键x
时的默认值。
答案 1 :(得分:1)
我认为你太过于复杂。你试图在你真正想要的事情发生之前用一些不存在的东西替换一个字符串。
试试这个:
def replace_all(text, dic):
return ''.join([dic[i] for i in text])
答案 2 :(得分:1)
我添加了一些印刷语句来显示正在发生的事情。
>>> def replace_all(text, dic):
print text
for i, j in dic.iteritems():
text = text.replace(i, j)
print "i: %s, j: %s, text: %s" % (i,j,text)
return text
>>> var2 = replace_all(var, ldic3)
t
i: 0, j: -54, text: t
i: 2, j: -56, text: t
i: 4, j: -58, text: t
i: 6, j: -60, text: t
i: 8, j: -62, text: t
i: B, j: -29, text: t
i: D, j: -31, text: t
i: F, j: -33, text: t
i: H, j: -35, text: t
i: J, j: -37, text: t
i: L, j: -39, text: t
i: N, j: -41, text: t
i: P, j: -43, text: t
i: R, j: -45, text: t
i: T, j: -47, text: t
i: V, j: -49, text: t
i: X, j: -51, text: t
i: Z, j: -53, text: t
i: b, j: -03, text: t
i: d, j: -05, text: t
i: f, j: -07, text: t
i: h, j: -09, text: t
i: j, j: -11, text: t
i: l, j: -13, text: t
i: n, j: -15, text: t
i: p, j: -17, text: t
i: r, j: -19, text: t
i: t, j: -21, text: -21
i: v, j: -23, text: -21
i: x, j: -25, text: -21
i: z, j: -27, text: -21
i: 1, j: -55, text: -2-55
i: 3, j: -57, text: -2-55
i: 5, j: -59, text: -2--59-59
i: 7, j: -61, text: -2--59-59
i: 9, j: -63, text: -2--5-63-5-63
i: A, j: -28, text: -2--5-63-5-63
i: C, j: -30, text: -2--5-63-5-63
i: E, j: -32, text: -2--5-63-5-63
i: G, j: -34, text: -2--5-63-5-63
i: I, j: -36, text: -2--5-63-5-63
i: K, j: -38, text: -2--5-63-5-63
i: M, j: -40, text: -2--5-63-5-63
i: O, j: -42, text: -2--5-63-5-63
i: Q, j: -44, text: -2--5-63-5-63
i: S, j: -46, text: -2--5-63-5-63
i: U, j: -48, text: -2--5-63-5-63
i: W, j: -50, text: -2--5-63-5-63
i: Y, j: -52, text: -2--5-63-5-63
i: a, j: -02, text: -2--5-63-5-63
i: c, j: -04, text: -2--5-63-5-63
i: e, j: -06, text: -2--5-63-5-63
i: g, j: -08, text: -2--5-63-5-63
i: i, j: -10, text: -2--5-63-5-63
i: k, j: -12, text: -2--5-63-5-63
i: m, j: -14, text: -2--5-63-5-63
i: o, j: -16, text: -2--5-63-5-63
i: q, j: -18, text: -2--5-63-5-63
i: s, j: -20, text: -2--5-63-5-63
i: u, j: -22, text: -2--5-63-5-63
i: w, j: -24, text: -2--5-63-5-63
i: y, j: -26, text: -2--5-63-5-63
所以你看,它正确地找到t
并用-21
替换它。哦,但是它也会找到1
并用-55
替换它。然后两个5
都被-59
取代。最后,9
被-63
取代。对于解决方案,TerryA's answer和Ishmael's answer看起来都不错。
答案 3 :(得分:1)
使用map()
方法只需get()
到词典。
result = ''.join(map(ldic3.get, var))
答案 4 :(得分:0)
这不是错误。你必须记住't'不会是dict迭代中的第一个,也不是最后一个项目。
我编辑了您的代码以跟踪错误:
def replace_all(text, dic):
for i, j in dic.iteritems():
text2 = text.replace(i, j)
if text2 != text:
print(i)
text = text2
return text
打印出以下内容
t
1
5
9
在此处构建代码时,您必须考虑代码中可能存在的错误:此处您只想替换“t”,而不是替换事件发生后的所有内容。
这意味着,它首先将“t”替换为“-21”,然后将“1”替换为“-55”,依此类推。您可能需要添加break语句,或明确更改 1 值。