【发布时间】:2013-09-05 09:12:16
【问题描述】:
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])
如何去掉标点符号!!我不知道在哪里放置那条线? 有人可以修改我的代码以删除除字母之外的所有内容吗?谢谢
【问题讨论】:
-
strip方法只删除字符串开头和结尾的字符。另外,我认为打开f然后重新分配f是一个坏主意。 -
@AshwiniChaudhary:有趣的是,该页面没有 Python 3 解决方案。我加了一个。
标签: python python-3.x