2012-09-25 16 views
-3

我有特殊字符的列表:如何在Python中表示新行?

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

我需要包括某種形式的字符符號的當用戶按下輸入按鈕的新線。我試過'\ n',但這並沒有奏效。有任何想法嗎?

更多信息BELOW

對不起,我應該已經指定。我正在製作一個基本的加密/解密應用程序。它所做的是將角色轉移到右邊的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() 
+2

'\ n' * *是換行符的符號。請顯示那些不起作用的代碼。 –

+1

什麼平臺?你如何捕捉用戶的按鍵? –

+5

你是什麼意思,「沒有工作」? – orlp

回答

0

大多數文本框(主要是Windows)需要\r\n切換到下一行。僅靠\n是不夠的。

+0

給它一個鏡頭,無濟於事。 :/ – user1675111

0

我仍然認爲你應該使用string以提高可讀性。但還是:

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

new linecarriage returncarriage return and new linetab

+0

沒有工作,對不起。 :/ – user1675111

+0

什麼都不起作用?你如何使用你的列表? –

+0

查看原貼 – user1675111