如何从文本文件中删除标点符号

时间:2013-09-02 09:48:08

标签: python python-3.x

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])

如何删除标点!!我无法弄清楚该线的位置? 有人可以修改我的代码以删除除字母之外的所有内容吗?谢谢

1 个答案:

答案 0 :(得分:6)

使用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)