3
import collections
import string
with open('cipher.txt') as f:
  f = f.read().replace(' ', '').replace('\n','').lower()
  f = f.strip(string.punctuation)

cnt = collections.Counter(f.replace(' ', ''))
for letter in sorted(cnt):
  print(letter, cnt[letter])

怎么去掉标点符号!!我不知道在哪里放置那条线?有人可以修改我的代码以删除除字母之外的所有内容吗?谢谢你

4

1 回答 1

8

用于str.translate()删除代码点;None删除任何映射到的代码点:

remove = dict.fromkeys(map(ord, '\n ' + string.punctuation))
f.translate(remove)

dict.fromkeys()方法可以轻松创建将所有键映射到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)
于 2013-09-02T09:51:38.880 回答