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


问题内容

这个问题已经在这里有了答案

从字符串中删除标点符号的最佳方法 (26个答案)

7年前关闭。

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

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


问题答案:

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