-3

我有一个特殊字符列表:

specialCharList=['`','~','!','@','#','$','%','^',
                         '&','*','(',')','-','_','+','=',
                         '|','','{','}','[',']',';',':',
                         '"',',','.','<','>','/','?',"'",'\\',' ']

当用户按下输入按钮换行时,我需要包含某种字符表示法。我试过'\n',但没有奏效。有任何想法吗?

更多信息如下

对不起,我应该指定的。我正在制作一个基本的加密/解密应用程序。它所做的是将字符向右移动 6 个位置并输出。

例如 abc 将输出为 ghi 例如 123 将输出为 789

我用特殊字符做了同样的事情。使用下面的列表来查看它是如何工作的。

specialCharList=['`','~','!','@','#','$','%','^',
                         '&','*','(',')','-','_','+','=',
                         '|','','{','}','[',']',';',':',
                         '"',',','.','<','>','/','?',"'",'\\',' ']

比如~!将输出为 ^&

虽然当有人在要加密的文本框中输入和组合文本、数字和特殊字符时一切正常,但如果有人输入新行(例如按回车键),我会收到错误消息。

index=specialCharList.index(tbInput[i])

ValueError: u'\n' 不在列表中

完整代码如下。

import wx
import os
class mainForm(wx.Frame):
    def __init__(self,parent,id):
        wx.Frame.__init__(self,parent,id,'Encryption Tool v2',size=(270,300))
        panel=wx.Panel(self)
        #Setting up controls
        wx.StaticText(panel,-1,'Enter Text Below',(10,10),(200,25))
        self.tbInput=wx.TextCtrl(panel,-1,'',(10,30),(250,220),wx.TE_MULTILINE)
        self.rdEncrypt=wx.RadioButton(panel,-1,'Encrypt',(10,250),(200,-1))
        self.rdDecrypt=wx.RadioButton(panel,-1,'Decrypt',(10,270),(200,-1))
        btnExecute=wx.Button(panel,-1,'Execute',(181,252),(80,-1))
        btnExecute.Bind(wx.EVT_BUTTON,self.encryptionDecryption)
    def encryptionDecryption(self,event):
        tbInput=self.tbInput.GetValue()
        rdEncrypt=self.rdEncrypt.GetValue()
        rdDecrypt=self.rdDecrypt.GetValue()
        if rdEncrypt==True and tbInput!='':
            #copy encryption code below
            encryptedStr=''
            alphabet=['X','M','y','B','e','f','N','D','i','Q',
                 'k','u','Z','J','s','A','q','Y','E','P','S',
                 'v','w','a','U','z','p','d','C','h','o','F',
                 'G','H','I','n','K','W','b','g','O','t','j',
                 'R','l','T','c','V','L','x','r','m']
            specialCharList=['`','~','!','@','#','$','%','^',
                             '&','*','(',')','-','_','+','=',
                             '|','','{','}','[',']',';',':',
                             '"',',','.','<','>','/','?',"'",'\\',' ',]
            for i in range(0,len(tbInput)):
                if tbInput[i].isalpha():
                    index=alphabet.index(tbInput[i])
                    if index+6>len(alphabet)-1:
                        index=5+(index-(len(alphabet)-1))
                        encryptedStr+=alphabet[index]
                    else:
                        encryptedStr+=alphabet[index+6]
                elif tbInput[i].isdigit():
                    if int(tbInput[i])+6>9:
                        encryptedStr+=str(-1+(int(tbInput[i])+6)-9)
                    else:
                        encryptedStr+=str(int(tbInput[i])+6)
                else:
                    index=specialCharList.index(tbInput[i])
                    if index+6>len(specialCharList)-1:
                        index=5+(index-(len(specialCharList)-1))
                        encryptedStr+=specialCharList[index]
                    else:
                        encryptedStr+=specialCharList[index+6]
            #print 'Encrypted Text: '+encryptedStr
            #text file here
            e=open('encryptedText.txt', 'w')
            e.write(encryptedStr)
            e.close()
            if os.name == 'nt':
                os.system('notepad ecryptedText.txt&')
            elif os.name == 'posix':
                os.system('gedit decryptedText.txt&')
            os.system('gedit encryptedText.txt&')
        elif rdDecrypt==True and tbInput!='':
            #copy code for decryption below
            decryptedStr=''
            alphabet=['X','M','y','B','e','f','N','D','i','Q',
                 'k','u','Z','J','s','A','q','Y','E','P','S',
                 'v','w','a','U','z','p','d','C','h','o','F',
                 'G','H','I','n','K','W','b','g','O','t','j',
                 'R','l','T','c','V','L','x','r','m']
            specialCharList=['`','~','!','@','#','$','%','^',
                             '&','*','(',')','-','_','+','=',
                             '|','','{','}','[',']',';',':',
                             '"',',','.','<','>','/','?',"'",'\\',' ']
            for i in range(0,len(tbInput)):
                if tbInput[i].isalpha():
                    index=alphabet.index(tbInput[i])
                    if index-6>len(alphabet)-1:
                        index=5+(index-(len(alphabet)-1))
                        decryptedStr+=alphabet[index]
                    else:
                        decryptedStr+=alphabet[index-6]
                elif tbInput[i].isdigit():
                    if int(tbInput[i])-6<0:
                        decryptedStr+=str(-1+(int(tbInput[i])-6)+11)
                    else:
                        decryptedStr+=str(int(tbInput[i])-6)
                else:
                    index=specialCharList.index(tbInput[i])
                    if index-6>len(specialCharList)-1:
                        index=5+(index-(len(specialCharList)-1))
                        decryptedStr+=specialCharList[index]
                    else:
                        decryptedStr+=specialCharList[index-6]
            #print 'Decrypted Text: '+decryptedStr
            #text file here
            d=open('decryptedText.txt', 'w')
            d.write(decryptedStr)
            d.close()
            if os.name == 'nt':
                os.system('notepad ecryptedText.txt&')
            elif os.name == 'posix':
                os.system('gedit decryptedText.txt&')
            os.system('gedit encryptedText.txt&')
        else:
            message=wx.MessageDialog(None, 'Please enter text for encryption/decryption','No Text Found',wx.OK|wx.ICON_INFORMATION)
            message.ShowModal()
            message.Destroy()
if __name__=='__main__':
    encryptionToolv2=wx.PySimpleApp()
    frame=mainForm(parent=None,id=-1)
    frame.Show()
    encryptionToolv2.MainLoop()
#usrInput=raw_input('Please enter your text.\n> ')
#eOrD=raw_input('Do you want to encrypt or decrypt? (e or d)\n> ')
#if eOrD=='e' or eOrD=='E':
#    encryption()
#elif eOrD=='d' or eOrD=='D':
#    decryption()
4

2 回答 2

0

大多数文本框(主要是 Windows)需要\r\n切换到下一行。\n单独是不够的。

于 2012-09-25T14:36:40.663 回答
0

我仍然认为您应该使用字符串来提高可读性。但仍然:

specialCharacters += ["\n", "\r", "\r\n", "\t"]

对于new line, carriage return, carriage return and new line, tab.

于 2012-09-25T14:41:05.007 回答