python中如何表示_新行在Python中是如何表示的?

我有一份特殊人物名单:specialCharList=['`','~','!','@','#','$','%','^',

'&','*','(',')','-','_','+','=',

'|','','{','}','[',']',';',':',

'"',',','.','','/','?',"'",'\\',' ']

我需要包括一些字符表示法,当用户按下enter按钮输入新行时。我试过'\n',但没用。有什么想法吗?在

下面是更多信息

对不起,我应该指定的。我在做一个基本的加密/解密应用程序。它的作用是将字符移到右边6个位置,然后输出。在

例如,abc将作为ghi输出

e、 输出为123克789

我也用特殊字符做了同样的事情。使用下面的列表查看它是如何工作的。在specialCharList=['`','~','!','@','#','$','%','^',

'&','*','(',')','-','_','+','=',

'|','','{','}','[',']',';',':',

'"',',','.','','/','?',"'",'\\',' ']

例如~!将输出为^&amp

当有人在文本框中输入文本、数字和特殊字符的组合并进行加密时,一切正常,但如果有人输入了一行新行(例如,按回车键),我会得到一个错误。在index=specialCharList.index(tbInput[i])

值错误: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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值