使用
str.translate()删除代码点;
None删除任何映射到的代码点:
remove = dict.fromkeys(map(ord, 'n ' + string.punctuation))f.translate(remove)
使用
dict.fromkeys()class方法可以轻松创建将所有键映射到的字典
None。
演示:
>>> import string>>> remove = dict.fromkeys(map(ord, 'n ' + string.punctuation))>>> sample = 'The quick brown fox, like, totally jumped, man!'>>> sample.translate(remove)'Thequickbrownfoxliketotallyjumpedman'
调整为您的代码:
remove = dict.fromkeys(map(ord, 'n ' + string.punctuation))with open('cipher.txt') as inputfile: f = inputfile.read().translate(remove)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)