我正在编写一段代码,以使用Caesar Cipher方法对邮件进行加密,如果我想对句子进行拆分(如果遇到特殊字符的话),则会弹出以下错误消息。
我尝试了其他方法,例如line.split(arg)
,但是问题是它只能在我想输入数十个参数时接受一个参数,所以到目前为止,唯一的选择似乎是re.split('args [seperated by '|']', str)
。但是,
import re, textwrap
def csr():
avalin = input('\n...\tPlease input the value of a: ')
fcharin = input('...\tDo you want to chage foreign characters also [Y/N]? ')
tmp = []
strout = ''
tmpstr = []
if fcharin.lower() == 'n' or fcharin.lower() == 'no':
tmpstr = (re.split('!|"|#|$|%|&|\'|(|)|*|+|,|-|.|/|:|;|<|=|>|?|@|[|\|]|^|_|`|{|||}|~|, |. |; |: |? |! | (|) | + | - | * | / | |\n|\*', strin))
else:
tmpstr.append(strin)
for item in tmpstr:
for char in item:
tmp.append((int(avalin)+(ord(char)-97))%26)
for item in tmp:
strout += chr(item+97)
wrlimit = textwrap.TextWrapper(width = 80)
wrlist = wrlimit.wrap(text=strout)
for line in wrlist:
print(line)
我希望输入根据上面tmpstr
中给出的字符进行拆分(应该返回一个数组),但是会弹出此错误:
File "main.py", line 47, in <module>
main()
File "main.py", line 44, in main
encrypt()
File "C:\Users\warre\OneDrive\Desktop\School\2018-19\Semester 2\Physical Science\Science Project 2019\Science Project 2019\encrypt.py", line 55, in encrypt
tmpstr = (re.split('!|"|#|$|%|&|\'|(|)|*|+|,|-|.|/|:|;|<|=|>|?|@|[|\|]|^|_|`|{|||}|~|, |. |; |: |? |! | (|) | + | - | * |
/ | |\n|\*', strin))
File "C:\Programs\Python\Python37\lib\re.py", line 213, in split
return _compile(pattern, flags).split(string, maxsplit)
File "C:\Programs\Python\Python37\lib\re.py", line 286, in _compile
p = sre_compile.compile(pattern, flags)
File "C:\Programs\Python\Python37\lib\sre_compile.py", line 764, in compile
p = sre_parse.parse(p, flags)
File "C:\Programs\Python\Python37\lib\sre_parse.py", line 930, in parse
p = _parse_sub(source, pattern, flags & SRE_FLAG_VERBOSE, 0)
File "C:\Programs\Python\Python37\lib\sre_parse.py", line 426, in _parse_sub
not nested and not items))
File "C:\Programs\Python\Python37\lib\sre_parse.py", line 651, in _parse
source.tell() - here + len(this))
re.error: nothing to repeat at position 18 (line 1, column 19)
是否有更方便的拆分句子的方法,或者有使用Caesar Cipher加密消息的快捷功能吗?